pub struct Circuit { /* private fields */ }
Expand description
A quantum circuit
Struct Circuit represents a quantum circuit, holding a quantum state and the operations to be performed on it.
Implementations§
Source§impl Circuit
impl Circuit
Sourcepub fn new(nr_qbits: usize, nr_cbits: usize) -> Self
pub fn new(nr_qbits: usize, nr_cbits: usize) -> Self
Create a new circuit.
Create a new (empty) quantum circuit, with nr_qbits
quantum bits and
nr_cbits
classical bits.
Sourcepub fn is_stabilizer_circuit(&self) -> bool
pub fn is_stabilizer_circuit(&self) -> bool
Return whether this circuit is a stabilizer circuit
Sourcepub fn cstate(&self) -> Option<&Array1<u64>>
pub fn cstate(&self) -> Option<&Array1<u64>>
The classical register.
Return a reference to the classical bit register, containing the results
of any measurements made on the system. If no experiment has been run
yet, None
is returned.
Sourcepub fn add_gate<G>(&mut self, gate: G, bits: &[usize]) -> Result<()>where
G: CircuitGate + 'static,
pub fn add_gate<G>(&mut self, gate: G, bits: &[usize]) -> Result<()>where
G: CircuitGate + 'static,
Add a gate.
Append a n
-ary gate gate
, operating on the n
qubits in bits
, to
this circuit.
Sourcepub fn add_conditional_gate<G>(
&mut self,
control: &[usize],
target: u64,
gate: G,
qbits: &[usize],
) -> Result<()>where
G: CircuitGate + 'static,
pub fn add_conditional_gate<G>(
&mut self,
control: &[usize],
target: u64,
gate: G,
qbits: &[usize],
) -> Result<()>where
G: CircuitGate + 'static,
Add a conditional gate.
Append a n
-ary gate gate
, that will operate on the n
qubits in
bits
to this circuit. The gate will only be applied only when the
classical bits with indices from control
form the target word target
.
The bit at the position of the first index in control
is interpreted
as the most significant bit to check.
Sourcepub fn measure_basis(
&mut self,
qbit: usize,
cbit: usize,
basis: Basis,
) -> Result<()>
pub fn measure_basis( &mut self, qbit: usize, cbit: usize, basis: Basis, ) -> Result<()>
Add a measurement
Add measurement of qubit qbit
in basis basis
, into classical bit
cbit
, to this circuit.
Sourcepub fn measure_x(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn measure_x(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli X
basis, into classical
bit cbit
to this circuit.
Sourcepub fn measure_y(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn measure_y(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli Y
basis, into classical
bit cbit
to this circuit.
Sourcepub fn measure_z(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn measure_z(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli Z
basis, into classical
bit cbit
to this circuit.
Sourcepub fn measure(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn measure(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
into classical bit cbit
to this circuit.
This is an alias for measure_z()
.
Sourcepub fn measure_all_basis(&mut self, cbits: &[usize], basis: Basis) -> Result<()>
pub fn measure_all_basis(&mut self, cbits: &[usize], basis: Basis) -> Result<()>
Add a measurement.
Add the measurement of all qubits in the quantum state into the classical
bits cbits
. Measurement is done in basis basis
.
Sourcepub fn measure_all(&mut self, cbits: &[usize]) -> Result<()>
pub fn measure_all(&mut self, cbits: &[usize]) -> Result<()>
Add a measurement.
Add the measurement of all qubits in the quantum state into the classical
bits cbits
. Measurement is done in the Pauli Z
basis.
Sourcepub fn peek_basis(
&mut self,
qbit: usize,
cbit: usize,
basis: Basis,
) -> Result<()>
pub fn peek_basis( &mut self, qbit: usize, cbit: usize, basis: Basis, ) -> Result<()>
Add a measurement.
Add the measurement of qubit qbit
in the quantum state into the
classical bit cbit
. Measurement is done in basis basis
, without
collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn peek_x(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn peek_x(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli X
basis, into classical
bit cbit
to this circuit, without collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn peek_y(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn peek_y(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli Y
basis, into classical
bit cbit
to this circuit, without collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn peek_z(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn peek_z(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli Z
basis, into classical
bit cbit
to this circuit, without collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn peek(&mut self, qbit: usize, cbit: usize) -> Result<()>
pub fn peek(&mut self, qbit: usize, cbit: usize) -> Result<()>
Add a measurement.
Add measurement of qubit qbit
in the Pauli Z
basis, into classical
bit cbit
to this circuit, without collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn peek_all_basis(&mut self, cbits: &[usize], basis: Basis) -> Result<()>
pub fn peek_all_basis(&mut self, cbits: &[usize], basis: Basis) -> Result<()>
Add a measurement.
Add the measurement of all qubits in the quantum state into the classical
bits cbits
. Measurement is done in basis basis
, without
collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn peek_all(&mut self, cbits: &[usize]) -> Result<()>
pub fn peek_all(&mut self, cbits: &[usize]) -> Result<()>
Add a measurement.
Add the measurement of all qubits in the quantum state into the classical
bits cbits
. Measurement is done in the Pauli Z
basis, without
collapsing the quantum state.
NOTE: this is not a physical process, and cannot be reproduced on a real
quantum computer.
Sourcepub fn reset(&mut self, qbit: usize) -> Result<()>
pub fn reset(&mut self, qbit: usize) -> Result<()>
Reset a qubit
Reset the qubit qbit
to |0⟩. This is done by measuring the bit, and
flipping it if the result is 1
, so this is potentially an expensive
operation.
Sourcepub fn reset_all(&mut self)
pub fn reset_all(&mut self)
Reset all qubits
Reset the entire quantum state of the circuit to |00…0⟩. The classical register is not affected.
Sourcepub fn h(&mut self, qbit: usize) -> Result<()>
pub fn h(&mut self, qbit: usize) -> Result<()>
Add a Hadamard gate.
Add a Hadamard gate operating on qubit qbit
, to this circuit.
Sourcepub fn x(&mut self, bit: usize) -> Result<()>
pub fn x(&mut self, bit: usize) -> Result<()>
Add a Pauli X gate.
Add a Pauli X gate operating on qubit bit
, to this circuit.
Sourcepub fn y(&mut self, bit: usize) -> Result<()>
pub fn y(&mut self, bit: usize) -> Result<()>
Add a Pauli Y gate.
Add a Pauli Y gate operating on qubit bit
, to this circuit.
Sourcepub fn z(&mut self, bit: usize) -> Result<()>
pub fn z(&mut self, bit: usize) -> Result<()>
Add a Pauli Z gate.
Add a Pauli Z gate operating on qubit bit
, to this circuit.
Sourcepub fn s(&mut self, bit: usize) -> Result<()>
pub fn s(&mut self, bit: usize) -> Result<()>
Add a phase gate
Add an S
phase gate operating on qubit bit
, to this circuit.
Sourcepub fn sdg(&mut self, bit: usize) -> Result<()>
pub fn sdg(&mut self, bit: usize) -> Result<()>
Add a phase gate
Add an S
\dagger
phase gate operating on qubit bit
, to this circuit.
Sourcepub fn rx<T>(&mut self, theta: T, bit: usize) -> Result<()>
pub fn rx<T>(&mut self, theta: T, bit: usize) -> Result<()>
Add a RX gate.
Add a R
X
(θ)
gate operating on qubit bit
, to this circuit.
Sourcepub fn ry<T>(&mut self, theta: T, bit: usize) -> Result<()>
pub fn ry<T>(&mut self, theta: T, bit: usize) -> Result<()>
Add a RY gate.
Add a R
Y
(θ)
gate operating on qubit bit
, to this circuit.
Sourcepub fn rz<T>(&mut self, lambda: T, bit: usize) -> Result<()>
pub fn rz<T>(&mut self, lambda: T, bit: usize) -> Result<()>
Add a RZ gate.
Add a R
Z
(λ)
gate operating on qubit bit
, to this circuit.
Sourcepub fn u1<T>(&mut self, lambda: T, bit: usize) -> Result<()>
pub fn u1<T>(&mut self, lambda: T, bit: usize) -> Result<()>
Add a U1 gate.
Add a U
1
(λ)
gate operating on qubit bit
, to this circuit.
Sourcepub fn u2<Tp, Tl>(&mut self, phi: Tp, lambda: Tl, bit: usize) -> Result<()>
pub fn u2<Tp, Tl>(&mut self, phi: Tp, lambda: Tl, bit: usize) -> Result<()>
Add a U2 gate.
Add a U
2
(ϕ, λ)
gate operating on qubit bit
, to this circuit.
Sourcepub fn u3<Tt, Tp, Tl>(
&mut self,
theta: Tt,
phi: Tp,
lambda: Tl,
bit: usize,
) -> Result<()>
pub fn u3<Tt, Tp, Tl>( &mut self, theta: Tt, phi: Tp, lambda: Tl, bit: usize, ) -> Result<()>
Add a U3 gate.
Add a U
3
(θ, ϕ, λ)
gate operating on qubit bit
, to this circuit.
Sourcepub fn cx(&mut self, control: usize, target: usize) -> Result<()>
pub fn cx(&mut self, control: usize, target: usize) -> Result<()>
Add a CX gate.
Add a C
X
gate, controlled by qubit control
and
operating on qubit target
, to this circuit.
Sourcepub fn barrier(&mut self, qbits: &[usize]) -> Result<()>
pub fn barrier(&mut self, qbits: &[usize]) -> Result<()>
Add a barrier
Add a barrier on the bits in bits
. No transformations on these bits
are allowed across this barrier.
Sourcepub fn execute(&mut self, nr_shots: usize) -> Result<()>
pub fn execute(&mut self, nr_shots: usize) -> Result<()>
Execute this circuit
Execute this circuit, performing its operations and measurements.
Measurements are made over nr_shots
executions of the circuit. This
function clears any previous states of the system (quantum or classical).
Sourcepub fn execute_with_rng<R: RngCore>(
&mut self,
nr_shots: usize,
rng: &mut R,
) -> Result<()>
pub fn execute_with_rng<R: RngCore>( &mut self, nr_shots: usize, rng: &mut R, ) -> Result<()>
Execute this circuit
Execute this circuit, performing its operations and measurements.
Measurements are made over nr_shots
executions of the circuit, using
random number generator rng
for sampling. This function clears any
previous states of the system (quantum or classical).
Sourcepub fn execute_with<R: RngCore>(
&mut self,
nr_shots: usize,
rng: &mut R,
q_state: QuStateRepr,
) -> Result<()>
pub fn execute_with<R: RngCore>( &mut self, nr_shots: usize, rng: &mut R, q_state: QuStateRepr, ) -> Result<()>
Execute this circuit
Execute this circuit, performing its operations and measurements.
Measurements are made over nr_shots
executions of the circuit, using
random number generator rng
for sampling. The initial quantum state of
the system is set to q_state
. The classical state is cleared before
execution.
Sourcepub fn reexecute(&mut self) -> Result<()>
pub fn reexecute(&mut self) -> Result<()>
Execute a circuit again.
Run this circuit again, starting with the state from the previous
execution. If this circuit has not been run before, a NotExecuted
error is returned.
Sourcepub fn reexecute_with_rng<R: Rng>(&mut self, rng: &mut R) -> Result<()>
pub fn reexecute_with_rng<R: Rng>(&mut self, rng: &mut R) -> Result<()>
Execute a circuit again.
Run this circuit again, starting with the state from the previous
execution, using random number generator rng
for sampling. If this
circuit has not been run before, a NotExecuted
error is returned.
Sourcepub fn histogram(&self) -> Result<HashMap<u64, usize, BuildIdentityHasher>>
pub fn histogram(&self) -> Result<HashMap<u64, usize, BuildIdentityHasher>>
Create a histogram of measurements.
Create a histogram of the measured classical bits. The n
bits in the
classical register are collected in a single u64
integer value. The
first bit in the classical register (at index 0) corresponds to the
least significant bit in the key; the last classical bit (at index n-1
)
to the most significant bit in the key. This function of course only works
when there are at most 64 bits in the register. If there are more, use
histogram_string()
.
Sourcepub fn histogram_vec(&self) -> Result<Vec<usize>>
pub fn histogram_vec(&self) -> Result<Vec<usize>>
Create a histogram of measurements.
Create a histogram of the measured classical bits. The n
bits in the
classical register are collected in a single usize
integer value,
which is used as an index in a vector. The bit order of the indices
is the same as in the histogram()
function. The vector is of length
2
n
, so use this function only for reasonably small
numbers of n
. For sparse collections, using histogram()
or
histogram_string
may be better.
Sourcepub fn histogram_string(&self) -> Result<HashMap<String, usize>>
pub fn histogram_string(&self) -> Result<HashMap<String, usize>>
Create a histogram of measurements.
Create a histogram of the measured classical bits. The n
bits in the
classical register are collected in a string key, with the last character
in the key corresponding to the first bit (at index 0) in the classical
register and vice versa.
Sourcepub fn open_qasm(&self) -> Result<String>
pub fn open_qasm(&self) -> Result<String>
Export to OpenQasm
Export this circuit to a program in OpenQasm format. On a successful
conversion, the result is Ok
with the program text. When the conversion
to OpenQasm fails, Err
with an error message is returned.
Auto Trait Implementations§
impl Freeze for Circuit
impl !RefUnwindSafe for Circuit
impl !Send for Circuit
impl !Sync for Circuit
impl Unpin for Circuit
impl !UnwindSafe for Circuit
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more