Struct Circuit

Source
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

Source

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.

Source

pub fn nr_qbits(&self) -> usize

The number of quantum bits in this circuit

Source

pub fn nr_cbits(&self) -> usize

The number of classical bits in this circuit

Source

pub fn is_stabilizer_circuit(&self) -> bool

Return whether this circuit is a stabilizer circuit

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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().

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn h(&mut self, qbit: usize) -> Result<()>

Add a Hadamard gate.

Add a Hadamard gate operating on qubit qbit, to this circuit.

Source

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.

Source

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.

Source

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.

Source

pub fn s(&mut self, bit: usize) -> Result<()>

Add a phase gate

Add an S phase gate operating on qubit bit, to this circuit.

Source

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.

Source

pub fn rx<T>(&mut self, theta: T, bit: usize) -> Result<()>
where Parameter: From<T>,

Add a RX gate.

Add a RX(θ) gate operating on qubit bit, to this circuit.

Source

pub fn ry<T>(&mut self, theta: T, bit: usize) -> Result<()>
where Parameter: From<T>,

Add a RY gate.

Add a RY(θ) gate operating on qubit bit, to this circuit.

Source

pub fn rz<T>(&mut self, lambda: T, bit: usize) -> Result<()>
where Parameter: From<T>,

Add a RZ gate.

Add a RZ(λ) gate operating on qubit bit, to this circuit.

Source

pub fn u1<T>(&mut self, lambda: T, bit: usize) -> Result<()>
where Parameter: From<T>,

Add a U1 gate.

Add a U1(λ) gate operating on qubit bit, to this circuit.

Source

pub fn u2<Tp, Tl>(&mut self, phi: Tp, lambda: Tl, bit: usize) -> Result<()>
where Parameter: From<Tp> + From<Tl>,

Add a U2 gate.

Add a U2(ϕ, λ) gate operating on qubit bit, to this circuit.

Source

pub fn u3<Tt, Tp, Tl>( &mut self, theta: Tt, phi: Tp, lambda: Tl, bit: usize, ) -> Result<()>
where Parameter: From<Tt> + From<Tp> + From<Tl>,

Add a U3 gate.

Add a U3(θ, ϕ, λ) gate operating on qubit bit, to this circuit.

Source

pub fn cx(&mut self, control: usize, target: usize) -> Result<()>

Add a CX gate.

Add a CX gate, controlled by qubit control and operating on qubit target, to this circuit.

Source

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.

Source

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).

Source

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).

Source

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.

Source

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.

Source

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.

Source

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().

Source

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 2n, so use this function only for reasonably small numbers of n. For sparse collections, using histogram() or histogram_string may be better.

Source

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.

Source

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.

Source

pub fn c_qasm(&self) -> Result<String>

Export to c-Qasm

Export this circuit to a program in c-Qasm format. On a successful conversion, the result is Ok with the program text. When the conversion to c-Qasm fails, Err with an error message is returned.

Source

pub fn latex(&self) -> Result<String>

Export to LaTeX

Export this circuit to LaTeX using the qcircuit package. On a successful conversion, the result is Ok with the LaTeX code. When the conversion to LaTeX 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V