Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 15

Building Quantum Circuits with Qiskit

Recommended resources

Welcome back! In our last lesson, you successfully set up your Qiskit development environment, which is your personal quantum laboratory. With the tools in place, we're now ready to start our first experiments.

This lesson bridges the gap between the theoretical gates we've discussed and their practical implementation. You'll learn how to "draw" quantum circuits using code, much like an electrical engineer designs a schematic before building a physical circuit. Our goal is to construct circuits in Qiskit by applying the single- and multi-qubit gates you're already familiar with conceptually.

1. The Anatomy of a Quantum Circuit in Qiskit

The fundamental object for building circuits in Qiskit is the QuantumCircuit class. Think of it as the canvas on which you'll place your qubits and gates.

To get started, you need to import this class from the qiskit library and create an instance of it. When creating a circuit, you specify the number of quantum bits (qubits) and, optionally, the number of classical bits you need. Classical bits are necessary to store the results of measurements, a topic we'll dive into in the next lesson.




# Import the QuantumCircuit class
from qiskit import QuantumCircuit




# Create a quantum circuit with 2 qubits and 2 classical bits
qc = QuantumCircuit(2, 2)

The video below gives a quick demonstration of creating a QuantumCircuit object.

How to program a quantum computer using Qiskit

This segment from IBM Technology's video introduces the basic structure of a Qiskit program, starting with the creation of a quantum circuit.

Watch the part of the video that explains how to create a quantum circuit. Notice the distinction between quantum registers (for computation) and classical registers (for storing results).

Once you have your QuantumCircuit object, you can visualize it using the .draw() method.




# Draw the empty circuit
qc.draw("mpl") 

This will produce a diagram showing your qubits (labeled q0, q1, etc.) and classical bits (labeled c0, c1, etc.) as horizontal wires. By default, all qubits are initialized in the state.

Quantum Circuit Diagram with Stages of Computation
A quantum circuit diagram visually represents the flow of information. Qubits are shown as horizontal lines, and gates are applied sequentially from left to right.

2. Applying Single-Qubit Gates

With our circuit canvas ready, we can start adding gates. In Qiskit, gates are methods of the QuantumCircuit object. You call the method corresponding to the gate you want and specify the index of the qubit to apply it to. Remember that qubit indexing starts at 0.

Let's apply some of the fundamental single-qubit gates we covered in Module 2.

Bits, gates, and circuits

This IBM Quantum Learning article provides clear, hands-on examples of applying single-qubit gates in Qiskit.

Read through the sections titled "X gate" and "H gate". For each gate, pay attention to: The Qiskit code used to create a circuit and apply the gate (e.g., qc.x(0)). The resulting circuit diagram produced by qc.draw("mpl"). You don't need to worry about the Statevector or plot_bloch_multivector parts for now; we'll cover simulation later. The focus here is on the circuit construction syntax.

As you can see from the reading, the syntax is very intuitive:

  • X gate (NOT): qc.x(qubit_index)
  • Hadamard gate: qc.h(qubit_index)
  • Z gate: qc.z(qubit_index)
  • S and T phase gates: qc.s(qubit_index) and qc.t(qubit_index)

3. Applying Multi-Qubit Gates

Multi-qubit gates are just as easy to apply. The most important two-qubit gate is the Controlled-NOT (CNOT or CX) gate. Its application method requires two arguments: the control qubit index and the target qubit index.

The CNOT gate acts like a conditional switch: if the control qubit is in the state , it applies an X gate (a "NOT") to the target qubit. If the control is , it does nothing.

Bits, gates, and circuits

These sections introduce multi-qubit circuits and the syntax for the CNOT gate.

Read the sections "Multi-qubit quantum circuit" and "CNOT gate". In the first part, notice how creating a two-qubit circuit and applying gates to each qubit works. In the second part, focus on the qc.cx(control_qubit, target_qubit) syntax and observe how the gate is represented in the circuit diagram. Again, you can skip the simulation and statevector parts for this lesson.

Other common multi-qubit gates you might encounter include:

  • SWAP gate: qc.swap(qubit1, qubit2)
  • Toffoli gate (CCX): qc.ccx(control1, control2, target)

4. Constructing a Bell State

Now, let's combine a single-qubit gate and a multi-qubit gate to create one of the most important states in quantum computing: a Bell state. This state is the simplest example of quantum entanglement.

To create the Bell state , we follow a simple two-step recipe:

  1. Start with two qubits in the state.
  2. Apply a Hadamard gate to the first qubit (q0).
  3. Apply a CNOT gate with the first qubit (q0) as control and the second qubit (q1) as target.

The following video walks through this exact process.

Hello World | Coding with Qiskit 1.x | Programming on Quantum Computers

This Qiskit video demonstrates the "Hello, World!" of quantum circuits: creating a Bell state.

Watch the segment where the presenter maps the problem to a circuit and generates the Bell State. Observe how the qc.h(0) and qc.cx(0, 1) commands translate directly into the circuit diagram shown.

Here is the complete code to build and draw the Bell state circuit:




# Create a 2-qubit circuit
qc_bell = QuantumCircuit(2)




# 1. Apply Hadamard gate to q0
qc_bell.h(0)




# 2. Apply CNOT gate with q0 as control and q1 as target
qc_bell.cx(0, 1)




# Draw the circuit
qc_bell.draw("mpl")

The output will be a circuit diagram showing an H gate on the top wire followed by a CNOT gate connecting the top and bottom wires.

Example Quantum Circuit Diagram
A simple two-qubit circuit. This particular example shows a Hadamard gate on q0, a CNOT with q0 as control, a rotation on q1, and then measurement. Our Bell state circuit is the first part of this diagram (H and CNOT gates).

5. Scaling Up: The GHZ State

The principles you've just learned scale to any number of qubits. A great example is the Greenberger-Horne-Zeilinger (GHZ) state, which is an entangled state of three or more qubits. For three qubits, the state is .

The recipe is a natural extension of the Bell state:

  1. Apply a Hadamard gate to q0.
  2. Apply a CNOT from q0 to q1.
  3. Apply a CNOT from q0 to q2. (Or from q1 to q2 for a "ladder" structure).

In practice, we often write functions to generate circuits programmatically. The next video shows how to create a function that can build a GHZ circuit for any number of qubits. This is a powerful technique and a common practice in quantum programming.

Hello World | Coding with Qiskit 1.x | Programming on Quantum Computers

This clip demonstrates how to write a Python function to programmatically generate a circuit for an N-qubit GHZ state.

Watch the section that extends the "Hello World" example to an N-qubit GHZ state. Pay attention to how a for loop is used to create a "ladder" of CNOT gates. This shows the power of combining classical programming constructs with quantum circuit building.


6. Practice: Building a Quantum Half-Adder

To solidify your understanding, let's build a slightly more complex and practical circuit: a quantum half-adder. This circuit takes two input bits, A and B, and computes their sum (S) and a carry bit (C), just like in classical computing.

We will need four qubits:

  • q0: Input A
  • q1: Input B
  • q2: Output Sum (S)
  • q3: Output Carry (C)

The logic is as follows:

  • Sum (S): S = A XOR B. This can be implemented with two CNOT gates: cx(A, S) and cx(B, S).
  • Carry (C): C = A AND B. This is implemented with a Toffoli (CCX) gate: ccx(A, B, C).

Let's build it. Follow the code in the reading below.

Get started with Qiskit in the classroom

This resource provides a complete, step-by-step implementation of a quantum half-adder circuit.

Find the section titled "Qiskit patterns framework" and work through substep "1. Mapping". Read the explanation of how the XOR and AND operations are mapped to quantum gates (CNOTs and a Toffoli). Copy the code block provided into your own Jupyter Notebook. Try changing the input values for a and b (0 or 1) at the top of the code and re-running the cell to see how the initial X gates change. Examine the final circuit diagram. Can you trace the logic for the sum and carry bits?

This exercise demonstrates how to assemble a complete quantum circuit that performs a recognizable computational task, applying both single- and multi-qubit gates in a structured way.


Conclusion

In this lesson, you've taken your first major step into quantum programming. You've learned how to translate the abstract concept of a quantum circuit into concrete Qiskit code. This is the foundational skill upon which all quantum algorithms are built.

Key Takeaways:

  • The QuantumCircuit class is the canvas for building quantum circuits.
  • You create a circuit by instantiating this class, specifying the number of qubits and classical bits.
  • Single-qubit gates are applied using methods like qc.h(qubit_index) and qc.x(qubit_index).
  • Multi-qubit gates, like CNOT, are applied with methods like qc.cx(control, target).
  • You can combine these gates to construct meaningful circuits, such as those for generating Bell and GHZ states, or even for performing simple arithmetic like a half-adder.
  • The .draw("mpl") method provides an essential visualization of your circuit's structure.

Preview of the Next Lesson:
You can now build quantum circuits, but what do they do? A circuit diagram is just a plan. The next step is to execute that plan and see the result. In our next lesson, you will learn how to use Qiskit's Aer simulator to run your circuits and find out the quantum state they produce.