pub struct StateVector { /* private fields */ }Expand description
The state of an n-qubit register as 2^n complex amplitudes.
Gates change the state in place. Reuse one StateVector across runs: reset
does not allocate. See the module docs for the qubit order.
Implementations§
Source§impl StateVector
impl StateVector
Sourcepub fn new(qubits: usize) -> Result<Self, TooManyQubits>
pub fn new(qubits: usize) -> Result<Self, TooManyQubits>
Makes an n-qubit register in the state |0…0⟩.
§Errors
Returns TooManyQubits when qubits is more than MAX_QUBITS.
Sourcepub fn amplitudes(&self) -> &[Complex]
pub fn amplitudes(&self) -> &[Complex]
Returns the amplitudes, indexed by basis state.
Sourcepub fn mcz(&mut self, qubits: impl IntoIterator<Item = usize>)
pub fn mcz(&mut self, qubits: impl IntoIterator<Item = usize>)
Applies the multi-controlled Z gate on qubits: a basis state changes sign when all
of these qubits are 1.
The gate is symmetric, so no qubit is special as the target. One qubit gives Z. An
empty set changes the sign of every state, a global phase without physical effect.
§Panics
Panics when a qubit is not less than qubits.
Sourcepub fn probabilities(&self) -> impl Iterator<Item = f64> + '_
pub fn probabilities(&self) -> impl Iterator<Item = f64> + '_
Returns the measurement probability of each basis state, in index order.
Sourcepub fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize
pub fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize
Measures all qubits once and returns the basis-state index, without a state collapse.
The draw comes from rng, so a fixed seed gives a reproducible result. When rounding
leaves the probabilities a little below 1 in total, the last index takes the rest.