Qubits
A qubit is the fundamental unit of quantum information. Unlike a classical bit, which is either 0 or 1, a qubit can exist in a superposition of both states simultaneously until it is measured. Inq, qubits are represented by the q.Qubit type (an integer index into the simulator’s internal state vector). You allocate qubits through the simulator, not directly.
- Zero state
- One state
- Custom amplitudes
Allocates a qubit in the |0⟩ state. This is the standard starting point for most quantum circuits.Internally,
Zero() calls New(1, 0) — amplitude 1 for |0⟩ and 0 for |1⟩.What is normalization?
What is normalization?
A quantum state must satisfy the normalization condition: the sum of the squared magnitudes of all amplitudes must equal 1. This ensures that the total probability of all measurement outcomes adds up to 100%.When you write Then it divides each amplitude by the norm:You never need to normalize manually —
qsim.New(1, 2), you are providing raw amplitude values. The simulator computes the norm:q.New(...) always produces a valid quantum state.Counting qubits
Useqsim.NumQubits() to get the total number of qubits currently allocated in the simulator:
Zero(), One(), or New(...) adds one qubit to the simulator and expands the internal state vector.
Tensor product
When you allocate multiple qubits, the simulator stores the combined system as a single state vector computed via the tensor product of the individual qubit states. For two qubits, this produces a 4-dimensional vector with basis states |00⟩, |01⟩, |10⟩, and |11⟩. The dimension of the state vector is always 2ⁿ, where n is the number of qubits.Superposition
Superposition means a qubit has non-zero amplitude for more than one basis state at the same time. The Hadamard gate (H) is the standard way to create superposition from a |0⟩ state:
H, q0 has equal probability (0.5) of being measured as 0 or 1.
Entanglement
Entanglement is a correlation between qubits such that the state of one cannot be described independently of the other. A CNOT gate entangles two qubits:Reading state output
qsim.State() returns a []qubit.State slice. Each State prints in the format:
| Part | Meaning |
|---|---|
[11] | Binary representation of the basis state |
[ 3] | Integer index of the basis state (binary 11 = 3) |
( 0.7071 0.0000i) | Complex amplitude: real and imaginary parts |
0.5000 | Measurement probability = |amplitude|² |
State() to inspect a subset of the system:
Quantum gates
Gates are unitary transformations applied to one or more qubits. All gate methods on*Q accept variadic qubit arguments and return *Q for chaining.
I, H, X, Y, Z, S, T, R, RX, RY, RZ, U.
Available multi-qubit gates: CNOT, CCNOT, CCCNOT, CZ, CCZ, CR, Swap, QFT, InvQFT.
For arbitrary unitaries, use G() with a custom matrix, or C() for a controlled version:
Measurement collapse
Callingqsim.Measure() collapses the quantum state. The simulator samples an outcome probabilistically, zeroes out all amplitudes inconsistent with that outcome, and renormalizes. Subsequent calls to State() reflect the post-measurement state.
Reset and clone
qsim.Reset() returns specified qubits to the |0⟩ state by measuring them and applying X if the result was |1⟩:
qsim.Clone() returns a deep copy of the entire simulator, preserving all qubit states: