A multi-qubit state describes a system of n qubits with one complex amplitude for each possible measurement outcome. Because n binary digits produce 2^n outcomes, the state is naturally represented as a list of 2^n complex numbers. section 4.2 section 4.2.2
Use these rules when encoding a state in a simulator:
- Match each list index k with the integer representation of an n-bit outcome, where 0 ≤ k < 2^n. section 4.2.2
- Require a list length that is a power of two and amplitudes whose squared magnitudes sum to 1. section 4.2.2
- Read each amplitude z_k = a_k + ib_k through its probability and direction, using |z_k|^2 for the probability. section 4.2.2
- Initialize the default state with amplitude 1 at index 0 and 0 elsewhere. section 4.2.3
Applying a gate
A single-qubit gate transforms a multi-qubit state by recombining amplitude pairs. The target-qubit position determines which indices form pairs. Applying an X gate to target qubit 0 swaps each pair; the same operation on target qubit 2 produces a different pairing pattern. section 4.3 section 4.3.3
Listing 4.1 Function to check whether a list is a valid quantum state
from math import log2, ceil, floor
from util import is_close
def is_power_of_two(m):
return ceil(log2(m)) == floor(log2(m))
def prepare_state(*a):
state = [a[k] for k in range(len(a))]
assert(is_power_of_two(len(state))) #1
assert (is_close(sum([abs(state[k]) ** 2 for k in range(len(state))]),
1.0)) #2
return state #3
Inspect the validation helper that checks power-of-two length and normalized amplitude magnitude before returning a simulator state. section 4.2.2
