Skip to main content

Welcome to a Grasp lesson

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

Lesson 10

Building Quantum Circuits for State Vectors

Recommended resources

Welcome to the third lesson in our module on "Manipulating Qubits with Quantum Gates." In the last two lessons, we've built a solid understanding of the fundamental single-qubit gates, including the Pauli gates (X, Y, Z), the Hadamard (H) gate for creating superpositions, and the phase gates (S, T) for fine-grained rotations.

So far, we've only applied these gates one at a time. The real power of quantum computing, however, comes from combining them into sequences to perform complex computations. This lesson bridges that gap. Our goal is to compose sequences of single-qubit gates to compute a final state vector. We will learn the mathematical rule for combining gates and then verify our results by building and simulating these sequences in Qiskit.

1. The Mathematics of Gate Composition

When we apply a sequence of gates to a qubit in a circuit, we are performing a series of matrix-vector multiplications. For instance, if we apply gate and then gate to an initial state , the final state is calculated as:

Due to the associative property of matrix multiplication, we can group the operations like this:

This shows that applying a sequence of gates is mathematically equivalent to applying a single, combined gate .

An Important Rule: Notice the order. The gate applied first in the circuit () is the rightmost matrix in the product. The sequence of matrix multiplications is read from right to left, which is the reverse of how we typically read a circuit diagram.

Single-Qubit Quantum Circuit Example
A quantum circuit showing a sequence of gates applied to a qubit. The final state is calculated by multiplying the matrices for these gates, starting from the right (H) and moving left.

Example: Applying a NOT (X) gate, then a Hadamard (H) gate

Let's calculate the combined operation for applying an X gate followed by an H gate to the state .

  1. Initial state:

  2. Gate matrices:

  3. Compute the total unitary matrix . Since you've noted matrix multiplication is an area you'd like to reinforce, let's walk through it carefully.

    To find the element in the first row, first column of the result, we take the dot product of the first row of H and the first column of X:

    First row, second column:

    Second row, first column:

    Second row, second column:

    Putting it all together, the combined matrix is:

  4. Apply the total unitary to the initial state:

    This final state is the state.

Let's check this by applying the gates sequentially:

  • First, apply X to : .
  • Then, apply H to the result: .
    The results match perfectly. This confirms that multiplying the gate matrices first gives the correct overall transformation.

2. Composing Unitary Operations

The following video explains this principle of composition very clearly and presents an interesting example that reveals some of the non-intuitive power of quantum operations.

Single Systems | Understanding Quantum Information & Computation | Lesson 01

This section of the Qiskit video "Single Systems" explains how sequences of unitary operations are composed via matrix multiplication.

Please watch the segment from composition of unitary operations. Pay close attention to the explanation of the multiplication order and the fascinating "square root of NOT" gate example. This shows how a sequence of gates (Hadamard, S, Hadamard) can produce a new operation that, when applied twice, results in a NOT gate—something with no classical analogue.

The "square root of NOT" is a great example of how combining simple rotations can result in a more complex and powerful operation. It underscores that the final unitary matrix represents the net effect of the entire gate sequence.

Quantum Gate Reference Table
A useful reference for the matrix representations of the common gates we are combining in our circuits.

3. From Theory to Practice: Tracking the State in Qiskit

Now, let's put this into practice and use Qiskit to verify our manual calculations. A powerful feature for this is the statevector_simulator, which allows us to see the exact quantum state vector at any point in the circuit, before any measurement collapse occurs.

The following reading provides a clear, step-by-step guide to building a circuit in Qiskit and, most importantly, tracking the state vector as it evolves.

Circuits with Qiskit

This chapter from Prof. Hamzeh Roumani's "Quantum Computing Notes" shows how to construct and analyze a circuit using Qiskit.

Read the section titled "7. Circuits with Qiskit". Follow the five steps to build a circuit. Pay special attention to the subsection "Capturing the state ket". This introduces the save_statevector() command, which is exactly what we need to see how the state vector changes after each gate is applied.

Your Turn: Building and Simulating a Circuit

Let's apply what you've just read to verify our HX|0> calculation from earlier.

  1. Set up your Qiskit environment.
  2. Create a circuit with one quantum register and one classical register.
  3. Initialize the qubit to the state . This is the default, but using qc.initialize([1,0], q) is good practice.
  4. Save the initial statevector: qc.save_statevector(label='initial')
  5. Apply the X gate: qc.x(q[0])
  6. Save the statevector again: qc.save_statevector(label='after_x')
  7. Apply the H gate: qc.h(q[0])
  8. Save the final statevector: qc.save_statevector(label='final')

Now, use the statevector_simulator to execute the circuit and print the saved state vectors.

from qiskit import QuantumCircuit, Aer, execute
import numpy as np




# Create the circuit
qc = QuantumCircuit(1) # We don't need a classical register for simulation




# Initial state is |0> by default
qc.save_statevector(label='initial')




# Apply X gate
qc.x(0)
qc.save_statevector(label='after_x')




# Apply H gate
qc.h(0)
qc.save_statevector(label='final')




# Execute and get results
backend = Aer.get_backend('statevector_simulator')
result = execute(qc, backend).result()




# Print the state vectors
initial_state = result.data(0)['initial']
after_x_state = result.data(0)['after_x']
final_state = result.data(0)['final']

print("Initial state (|0>):", np.round(initial_state, 3))
print("State after X gate (|1>):", np.round(after_x_state, 3))
print("Final state (H|1> or |->):", np.round(final_state, 3))

When you run this code, you should see the final state printed as [ 0.707+0.j, -0.707+0.j], which corresponds to . This is exactly the state we calculated by hand, confirming our understanding of gate composition!


Conclusion

In this lesson, we made a significant step from knowing individual gates to understanding how they work together in a sequence. This is the foundation for building any quantum algorithm.

Key Takeaways:

  • A sequence of quantum gates applied to a state is equivalent to applying a single combined gate.
  • The matrix for this combined gate is found by multiplying the individual gate matrices.
  • The order of matrix multiplication is crucial and is the reverse of the gate order in a circuit diagram (right-to-left).
  • We can use Qiskit's statevector_simulator and the save_statevector() method to track the evolution of a qubit's state and verify our mathematical calculations.

Preview of the Next Lesson:
All the gates we've worked with so far operate on a single qubit. However, to perform computations that are more powerful than classical ones, we need qubits to interact. In the next lesson, we will introduce multi-qubit gates like the CNOT and SWAP gates. These are the fundamental building blocks that allow us to create entanglement and perform logic between qubits, unlocking a new level of computational capability.