This document explains every public function in the quantum_core library and describes the variables they accept.
Create a state vector representing |0...0> for a multi-qubit system.
number_of_qubits(int): number of qubits in the state. Must be a positive integer.- Returns: NumPy array of length
2**number_of_qubitswith the first element equal to1and all others0. - Raises:
TypeErrorifnumber_of_qubitsis not an integer.ValueErrorifnumber_of_qubits <= 0.
Create a basis state from a classical bit string.
bit_string(str): string of0and1characters, such as"00"or"101".- Returns: NumPy array representing the basis state
|bit_string>. - Raises:
TypeErrorifbit_stringis not a string.ValueErrorifbit_stringis empty.ValueErrorifbit_stringcontains characters other than0or1.
Create a normalized single-qubit state from amplitudes.
alpha(complex or numeric): amplitude for|0>.beta(complex or numeric): amplitude for|1>.- Returns: normalized NumPy array
[alpha, beta]. - Uses
normalize_stateinternally, so a zero or invalid state will raise an error.
Return the number of qubits represented by a state vector.
state(np.ndarray): one-dimensional NumPy array of length2**n.- Returns: integer
n. - Raises:
TypeErrorifstateis not a NumPy array.ValueErrorifstateis not one-dimensional or empty.ValueErrorif the array length is not a power of 2.
Validate the basic shape of a state vector.
state(np.ndarray): state vector.- Raises if the state is not a 1D NumPy array with nonzero length.
Normalize a state vector so the total probability equals 1.
state(np.ndarray): complex state vector.- Returns: normalized state vector.
- Raises:
TypeErrorifstateis not a NumPy array.ValueErrorif the state is empty, not one-dimensional, not length power of 2, or all-zero.
Validate that a state vector is normalized and correctly shaped.
state(np.ndarray): state vector.- Returns:
Trueif valid. - Raises:
TypeErrorifstateis not a NumPy array.ValueErrorif the shape is invalid, the length is not a power of 2, or the total probability differs from 1 by more than1e-8.
Get a human-readable list of nonzero amplitudes.
state(np.ndarray): valid, normalized state vector.- Returns: list of strings like
"(0.70710678+0j) |00>". - Uses
check_state_is_validinternally.
Get probability values for each basis state.
state(np.ndarray): valid state vector.- Returns: dictionary mapping bit strings to probabilities.
- For example,
{"00": 0.5, "11": 0.5}.
I: identity gate for one qubit.X: Pauli-X (bit flip).Y: Pauli-Y.Z: Pauli-Z (phase flip).H: Hadamard.S: phase gate withion|1>.T: phase gate withe^{i\pi/4}on|1>.CNOT: controlled-NOT on two qubits.CZ: controlled-Z on two qubits.
Validate that a quantum gate is a square unitary matrix sized for qubits.
gate(np.ndarray): 2D NumPy array.- Returns:
Trueif the gate is valid. - Raises:
TypeErrorifgateis not a NumPy array.ValueErrorifgateis not 2D, not square, empty, or has dimensions not a power of 2.ValueErrorif the gate is not unitary.
Create a one-qubit phase gate.
theta(float): phase angle in radians.- Returns:
[[1, 0], [0, exp(i * theta)]].
Create a rotation around the Y axis.
theta(float): rotation angle in radians.- Returns: a one-qubit
Ryunitary matrix.
Validate target qubit indices for a gate application.
number_of_qubits(int): total qubits in the current state.target_qubits(list[int]): qubit indices to apply the gate to.- Returns:
Trueif valid. - Raises:
TypeErrorif inputs have invalid types.ValueErrorif indices are empty, negative, out of range, or repeated.
Apply a quantum gate to selected qubits in a state vector.
state(np.ndarray): normalized state vector.gate(np.ndarray): unitary gate matrix.target_qubits(list[int]): qubit indices where the gate acts.- Returns: normalized new state after applying the gate.
- Raises:
ValueErrorif the gate size does not match the number of target qubits.
Internal helper that uses tensor contraction to apply the gate.
state(np.ndarray): state vector.gate(np.ndarray): gate matrix.target_qubits(list[int]): qubit indices.- Returns: new raw state vector before normalization.
Alias for probability_dictionary(state).
state(np.ndarray): valid state vector.- Returns: dictionary of outcome probabilities.
Validate qubit indices used in measurement.
state(np.ndarray): valid state vector.target_qubits(list[int] orNone): qubits to measure.- Returns: validated list of qubit indices.
- If
target_qubitsisNone, all qubits are measured.
Measure one or more qubits and collapse the state.
state(np.ndarray): valid state vector.target_qubits(list[int] orNone): qubits to measure.random_seed(int orNone): seed for reproducible randomness.- Returns: tuple
(chosen_result, collapsed_state).chosen_resultis a bit string like"0","10", or"11".collapsed_stateis the new normalized state after collapse.
Create all possible bit strings of a given length.
number_of_bits(int): number of qubits.- Returns: list of length
2**number_of_bitswith strings from"00"to"11".
Compute probability of a specific partial measurement outcome.
state(np.ndarray): valid state vector.target_qubits(list[int]): qubits being measured.wanted_result(str): bit string describing the measurement result ontarget_qubits.- Returns: float probability.
Collapse the state to a measurement result.
state(np.ndarray): valid state vector.target_qubits(list[int]): measured qubits.chosen_result(str): observed bit string.- Returns: new normalized state after zeroing inconsistent amplitudes.
from quantum_core.state import make_zero_state
from quantum_core.circuit import apply_gate
from quantum_core.gates import H, CNOT
from quantum_core.measurement import get_probabilities, measure
state = make_zero_state(2)
state = apply_gate(state, H, [0])
state = apply_gate(state, CNOT, [0, 1])
print(get_probabilities(state))
measured_bits, collapsed_state = measure(state, [0])
print(measured_bits)
print(get_probabilities(collapsed_state))import numpy as np
from quantum_core.state import make_one_qubit_state, normalize_state
from quantum_core.circuit import apply_gate
from quantum_core.gates import H, CNOT, X, Z
from quantum_core.measurement import measure
unknown_state = make_one_qubit_state(np.sqrt(0.3), np.sqrt(0.7) * np.exp(0.37j))
state = np.kron(unknown_state, np.array([1, 0, 0, 0], dtype=complex))
state = normalize_state(state)
state = apply_gate(state, H, [1])
state = apply_gate(state, CNOT, [1, 2])
state = apply_gate(state, CNOT, [0, 1])
state = apply_gate(state, H, [0])
alice_bits, state = measure(state, [0, 1], random_seed=4)
if alice_bits[1] == "1":
state = apply_gate(state, X, [2])
if alice_bits[0] == "1":
state = apply_gate(state, Z, [2])- All state vectors are represented as one-dimensional NumPy arrays of complex values.
- Gate matrices must be unitary and have dimensions
2**n x 2**n. - Qubit indices start at
0.