Writing code to simulate quantum states brings the mathematical abstractions into focus. These abstractions can be translated into executable Python scripts that implement vector operations. Qubits are described using Dirac notation and matrix multiplication. A quantum circuit is constructed in a programming environment. This object acts as a container for the sequence of operations, initialization, gates, and measurement, that we wish to perform on our qubits.
A basic quantum program consists of three stages. First, we initialize the system. By default, most quantum software development kits (SDKs) initialize qubits in the ground state . Second, we apply a series of gates to manipulate the state vector. Finally, we perform a measurement to collapse the state and record the result as a classical bit (0 or 1).
To store the measurement result, our circuit requires both a quantum register (to hold the qubit) and a classical register (to hold the output).
The following diagram illustrates the flow of a standard single-qubit circuit. The qubit travels from left to right, encountering a gate before being measured.
Flow of information in a basic quantum circuit involving initialization, manipulation, and measurement.
We begin with the Pauli-X gate. As established in previous sections, the X gate acts as a bit-flip. If we apply it to the initialized state , the state vector rotates to .
In Python, using a standard syntax structure common to libraries like Qiskit, the implementation follows these steps:
# Pseudo-code representation of circuit logic
circuit = QuantumCircuit(1, 1) # 1 Qubit, 1 Classical Bit
# Apply the Pauli-X gate
circuit.x(0)
# Measure qubit 0 and store in classical bit 0
circuit.measure(0, 0)
Because the X gate is deterministic when starting from a basis state, the outcome is predictable. The vector transforms from to . If we run this circuit on a simulator, we receive the result 1 with 100% probability.
The behavior becomes unique to quantum mechanics when we utilize the Hadamard gate (). This gate places the qubit into an equal superposition.
When we write code for this, the instructions look similar to the X gate example, but the computational reality is quite different.
# Circuit for Superposition
circuit = QuantumCircuit(1, 1)
# Apply Hadamard gate to create superposition
circuit.h(0)
# Measure
circuit.measure(0, 0)
In this scenario, the qubit exists in a state where the probability of measuring 0 is and the probability of measuring 1 is also 0.5.
A single execution of the code above yields a single binary digit: either 0 or 1. This single data point does not prove the qubit was in superposition. It only tells us what the state collapsed into. To verify the probability amplitudes, we must execute the circuit multiple times.
In quantum programming, we define a parameter often called shots. This represents the number of times the experiment is repeated. Common shot counts are 1024, 4096, or 8192. By aggregating the results of many shots, we build a probability distribution.
If we run the Hadamard circuit with 1000 shots, we expect approximately 500 zeros and 500 ones. Due to statistical variance, the actual numbers might be 489 and 511, but they will converge toward 50/50 as the shot count increases.
The following chart displays a typical result distribution from a simulator running a Hadamard gate experiment with 1024 shots.
Statistical distribution of measurement outcomes after applying a Hadamard gate to the ground state.
We can chain multiple gates together to perform more complex rotations. The order of operations is significant because matrix multiplication is not commutative. Applying an X gate followed by a Hadamard gate produces a different state vector than applying a Hadamard followed by an X gate.
Consider the operation sequence :
If we measure this state, the probabilities remain 50/50 because the Z gate only changes the phase (the sign), not the amplitude magnitude. This highlights an important limitation of measurement: we cannot directly observe the phase difference between and using a standard measurement in the Z-basis. We would need to apply another Hadamard gate before measurement to distinguish between these two states.
By practicing these circuit constructions, you verify the linear algebra predictions made in the previous sections. The ability to manipulate single qubits is the foundation for the next major leap in complexity: interacting two qubits to create entanglement.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with