pub struct QReg { /* private fields */ }
Expand description
The heart of QVNT
crate. It represents a set of entangle qubits,
their collective wavefunction and techniques for controlling quantum state.
Theoretically it could contain up to 64 qubits, however it would require more than 2_000_000 Terabytes of RAM. For a common computer with 4Gb RAM the limitation is 26 qubits. If you have more than 4Gb just remember, that 1 additional qubit require twice more RAM. More precise formula is:
MAX_QUBIT_COUNT = 24 + log2(MEM_CAPACITY_IN_GB)
For practice purposes it will be enough.
To create quantum computer state, e.g. with 10 qubits, you can use this code:
use qvnt::prelude::*;
let q = QReg::new(10);
The quantum register q
starts with state |0>.
To vary initial state of register, you may use with_state
modifier:
// it will create quantum register in state |123>
let q = QReg::with_state(10, 123);
After creation of quantum computer you would like to be able to control its state.
QVNT provide op
module, which contains an amount of quantum gates.
QReg::apply()
method is to apply sorry for tautology :) them:
// quantum gates change state, so register must be mutable
let mut q = QReg::new(2);
// controlled gates have to be unwrapped
let gate = op::h(0b01) * op::x(0b10).c(0b01).unwrap();
q.apply(&gate);
This is the example of entangled state, which means that if we will apply gate on or measure
first(second) qubit, second(first) will change it state automatically.
To show that, we will use get_probabilities
method.
It could show us the probabilities of each state in quantum register:
let mut q = QReg::new(2);
q.apply(&gate);
let prob: Vec<f64> = q.get_probabilities();
println!("{:?}", prob);
Output will be [0.5, 0.0, 0.0, 0.5]
, which means that quantum state consists only of states |00> and |11>.
Thus, measuring first qubit (|_0>
or |_1>
will always collapse second qubit to the same value.
So, this example is just a complicated version if flipping a coin example.
Implementations
sourceimpl Reg
impl Reg
sourcepub fn new(q_num: usize) -> Self
pub fn new(q_num: usize) -> Self
Create quantum register with a given number of bits. Initial value will be set to 0.
sourcepub fn with_state(q_num: usize, state: usize) -> Self
pub fn with_state(q_num: usize, state: usize) -> Self
Create quantum register with a given number of bits and an initial state
pub fn num(&self) -> usize
pub fn set_num(&mut self, q_num: usize)
sourcepub fn get_vreg_by(&self, mask: usize) -> Option<VReg>
pub fn get_vreg_by(&self, mask: usize) -> Option<VReg>
Acquire the VReg
for a specified part of quantum register.
sourcepub fn apply<Op>(&mut self, op: &Op)where
Op: Applicable,
pub fn apply<Op>(&mut self, op: &Op)where
Op: Applicable,
Apply quantum gate to register.
This method only works in single threading model.
To accelerate it you may use [apply_sync
].
sourcepub fn get_polar(&self) -> Vec<(f64, f64)>
pub fn get_polar(&self) -> Vec<(f64, f64)>
Return complex amplitudes of quantum states of register in polar form.
sourcepub fn get_probabilities(&self) -> Vec<f64>
pub fn get_probabilities(&self) -> Vec<f64>
Return probabilities of quantum states of register.
sourcepub fn get_absolute(&self) -> f64
pub fn get_absolute(&self) -> f64
Return absolute value of wavefunction of quantum register.
If you use gates from op
module, it always will be 1.
sourcepub fn measure_mask(&mut self, mask: usize) -> CReg
pub fn measure_mask(&mut self, mask: usize) -> CReg
Measure specified qubits into classical register. Wavefunction of quantum register will collapse after measurement.
sourcepub fn measure(&mut self) -> CReg
pub fn measure(&mut self) -> CReg
Measure all qubits into classical register. Wavefunction of quantum register will collapse after measurement.
sourcepub fn sample_all(&self, count: usize) -> Vec<usize>
pub fn sample_all(&self, count: usize) -> Vec<usize>
Make a histogram for quantum register.
This histogram also could be obtained by calling measure
count times.
But sample_all
does not collapse wavefunction and executes MUSH FASTER.
If you want to simulate the execution of quantum computer, you would prefer sample_all
.