pub struct Circuit { /* private fields */ }Expand description
A quantum circuit: an ordered list of Ops over n qubits and some
number of classical bits.
Gate methods take plain usize indices, return &mut Self, and so chain
fluently. They record operations without validating eagerly; call
Circuit::validate (which backends do automatically) to check all indices
at once.
§Examples
use everett::prelude::*;
let mut c = Circuit::new(3);
c.h(0).cnot(0, 1).cnot(1, 2); // a 3-qubit GHZ circuit
assert_eq!(c.len(), 3);Implementations§
Source§impl Circuit
impl Circuit
Sourcepub fn new(num_qubits: usize) -> Self
pub fn new(num_qubits: usize) -> Self
Creates an empty circuit over num_qubits qubits and no classical bits.
Sourcepub fn with_classical(num_qubits: usize, num_classical: usize) -> Self
pub fn with_classical(num_qubits: usize, num_classical: usize) -> Self
Creates an empty circuit with both a quantum and a classical register.
Sourcepub fn num_qubits(&self) -> usize
pub fn num_qubits(&self) -> usize
The number of qubits.
Sourcepub fn num_classical(&self) -> usize
pub fn num_classical(&self) -> usize
The number of classical bits.
Sourcepub fn gate1(&mut self, gate: Gate1, target: usize) -> &mut Self
pub fn gate1(&mut self, gate: Gate1, target: usize) -> &mut Self
Appends an arbitrary single-qubit gate on target.
Sourcepub fn gate2(&mut self, gate: Gate2, a: usize, b: usize) -> &mut Self
pub fn gate2(&mut self, gate: Gate2, a: usize, b: usize) -> &mut Self
Appends an arbitrary two-qubit gate on operands a and b.
Sourcepub fn controlled(
&mut self,
controls: &[usize],
gate: Gate1,
target: usize,
) -> &mut Self
pub fn controlled( &mut self, controls: &[usize], gate: Gate1, target: usize, ) -> &mut Self
Appends a single-qubit gate on target, controlled on all of controls.
Sourcepub fn compose(&mut self, sub: &Circuit) -> &mut Self
pub fn compose(&mut self, sub: &Circuit) -> &mut Self
Appends another circuit’s operations into this one, in place. Useful for
inlining a subroutine such as a width-k QFT.
The sub-circuit’s qubit and classical indices are used as-is, so they
must fit within this circuit’s registers (checked by Self::validate).
Sourcepub fn phase(&mut self, q: usize, lambda: f64) -> &mut Self
pub fn phase(&mut self, q: usize, lambda: f64) -> &mut Self
Appends a relative phase e^{i*lambda} on q.
Sourcepub fn cnot(&mut self, control: usize, target: usize) -> &mut Self
pub fn cnot(&mut self, control: usize, target: usize) -> &mut Self
Appends a CNOT with control and target.
Sourcepub fn cz(&mut self, a: usize, b: usize) -> &mut Self
pub fn cz(&mut self, a: usize, b: usize) -> &mut Self
Appends a controlled-Z on a and b (symmetric).
Sourcepub fn measure(&mut self, qubit: usize, into: usize) -> &mut Self
pub fn measure(&mut self, qubit: usize, into: usize) -> &mut Self
Measures qubit, storing the outcome in classical bit into.
Sourcepub fn x_if(&mut self, bit: usize, q: usize) -> &mut Self
pub fn x_if(&mut self, bit: usize, q: usize) -> &mut Self
Applies a Pauli-X on q if classical bit bit is set. The basic
feed-forward correction used by teleportation.
Sourcepub fn z_if(&mut self, bit: usize, q: usize) -> &mut Self
pub fn z_if(&mut self, bit: usize, q: usize) -> &mut Self
Applies a Pauli-Z on q if classical bit bit is set.
Sourcepub fn if_classic(&mut self, bit: usize, op: Op) -> &mut Self
pub fn if_classic(&mut self, bit: usize, op: Op) -> &mut Self
Runs op only if classical bit is set. General classical control.