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.Defining the Circuit StructureA 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 $|0\rangle$. 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.digraph G { rankdir=LR; node [shape=box, style=filled, color="#dee2e6", fontname="Arial"]; edge [color="#868e96"]; start [label="|0⟩", shape=circle, fillcolor="#b2f2bb", color="#8ce99a"]; gate [label="Unitary Operator\n(Gate)", fillcolor="#a5d8ff", color="#74c0fc"]; measure [label="Measurement\n(M)", fillcolor="#ffc9c9", color="#ff8787"]; output [label="Classical Bit\n(0 or 1)", shape=circle, fillcolor="#e9ecef", color="#ced4da"]; start -> gate [label=" q[0]"]; gate -> measure [label=" Transformed State"]; measure -> output [label=" Collapse"]; }Flow of information in a basic quantum circuit involving initialization, manipulation, and measurement.Implementing the X GateWe 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 $|0\rangle$, the state vector rotates to $|1\rangle$.In Python, using a standard syntax structure common to libraries like Qiskit, the implementation follows these steps:Initialize: Create a circuit with one qubit and one classical bit.Apply Gate: Call the X function on the targeted qubit index (0).Measure: Map the quantum state of qubit 0 to classical bit 0.# 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 $\begin{bmatrix} 1 \ 0 \end{bmatrix}$ to $\begin{bmatrix} 0 \ 1 \end{bmatrix}$. If we run this circuit on a simulator, we receive the result 1 with 100% probability.Creating Superposition with the Hadamard GateThe behavior becomes unique to quantum mechanics when we utilize the Hadamard gate ($H$). This gate places the qubit into an equal superposition.$$ H|0\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}} = |+\rangle $$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 $| \frac{1}{\sqrt{2}} |^2 = 0.5$ and the probability of measuring 1 is also 0.5.Simulation and Shot CountsA 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.{"layout": {"title": {"text": "Measurement Outcomes: Hadamard Gate (1024 Shots)", "font": {"family": "Arial", "size": 16}}, "xaxis": {"title": {"text": "Measured State"}, "tickfont": {"family": "Arial"}}, "yaxis": {"title": {"text": "Probability"}, "range": [0, 1]}, "plot_bgcolor": "#f8f9fa", "paper_bgcolor": "#ffffff", "margin": {"t": 40, "b": 40, "l": 50, "r": 20}, "width": 600, "height": 400}, "data": [{"x": ["|0⟩", "|1⟩"], "y": [0.514, 0.486], "type": "bar", "marker": {"color": ["#4dabf7", "#20c997"]}, "text": ["51.4%", "48.6%"], "textposition": "auto", "width": 0.4}]}Statistical distribution of measurement outcomes after applying a Hadamard gate to the ground state.Sequential OperationsWe 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 $Z H |0\rangle$:Start at $|0\rangle$.Apply $H$: Transforms to $|+\rangle = \frac{|0\rangle + |1\rangle}{\sqrt{2}}$.Apply $Z$: The Z gate flips the phase of the $|1\rangle$ component. The state becomes $|-\rangle = \frac{|0\rangle - |1\rangle}{\sqrt{2}}$.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 $|+\rangle$ and $|-\rangle$ 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.