Represent a qubit as data
Start with an object that owns a normalized state vector. The H gate transforms that vector; it does not assign a third boolean value.
import numpy as np
class Qubit:
def __init__(self, state=None):
self.state = np.array(state or [1, 0], dtype=complex)
self.state /= np.linalg.norm(self.state)
def probabilities(self):
return np.abs(self.state) ** 2
q = Qubit()
H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
q.state = H @ q.state
print(q.probabilities()) # [0.5 0.5]
Two objects are not automatically entangled
Two independent qubits form a product state: you can describe each object separately. Entanglement appears when the combined vector cannot be factored into one vector per object.
# basis order: |00>, |01>, |10>, |11>
bell = np.array([1, 0, 0, 1], dtype=complex) / np.sqrt(2)
print(np.abs(bell) ** 2) # [0.5, 0, 0, 0.5]
Each qubit has a 50/50 local view, while the pair only returns 00 or 11. The pair is the unit of state a simulator must track.
Debugging tip: never infer a multi-qubit state by concatenating independent observations. Build the joint vector first.
Now follow the state through the quantum execution lifecycle, or open the lab and build the Bell circuit.