Skip to main content

Circuit

Struct Circuit 

Source
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

Source

pub fn new(num_qubits: usize) -> Self

Creates an empty circuit over num_qubits qubits and no classical bits.

Source

pub fn with_classical(num_qubits: usize, num_classical: usize) -> Self

Creates an empty circuit with both a quantum and a classical register.

Source

pub fn num_qubits(&self) -> usize

The number of qubits.

Source

pub fn num_classical(&self) -> usize

The number of classical bits.

Source

pub fn ops(&self) -> &[Op]

The recorded operations.

Source

pub fn len(&self) -> usize

The number of operations.

Source

pub fn is_empty(&self) -> bool

Whether the circuit has no operations.

Source

pub fn gate1(&mut self, gate: Gate1, target: usize) -> &mut Self

Appends an arbitrary single-qubit gate on target.

Source

pub fn gate2(&mut self, gate: Gate2, a: usize, b: usize) -> &mut Self

Appends an arbitrary two-qubit gate on operands a and b.

Source

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.

Source

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

Source

pub fn h(&mut self, q: usize) -> &mut Self

Appends a Hadamard on q.

Source

pub fn x(&mut self, q: usize) -> &mut Self

Appends a Pauli-X on q.

Source

pub fn y(&mut self, q: usize) -> &mut Self

Appends a Pauli-Y on q.

Source

pub fn z(&mut self, q: usize) -> &mut Self

Appends a Pauli-Z on q.

Source

pub fn s(&mut self, q: usize) -> &mut Self

Appends an S gate on q.

Source

pub fn t(&mut self, q: usize) -> &mut Self

Appends a T gate on q.

Source

pub fn rx(&mut self, q: usize, theta: f64) -> &mut Self

Appends an X-rotation by theta on q.

Source

pub fn ry(&mut self, q: usize, theta: f64) -> &mut Self

Appends a Y-rotation by theta on q.

Source

pub fn rz(&mut self, q: usize, theta: f64) -> &mut Self

Appends a Z-rotation by theta on q.

Source

pub fn phase(&mut self, q: usize, lambda: f64) -> &mut Self

Appends a relative phase e^{i*lambda} on q.

Source

pub fn cnot(&mut self, control: usize, target: usize) -> &mut Self

Appends a CNOT with control and target.

Source

pub fn cz(&mut self, a: usize, b: usize) -> &mut Self

Appends a controlled-Z on a and b (symmetric).

Source

pub fn swap(&mut self, a: usize, b: usize) -> &mut Self

Appends a SWAP of a and b.

Source

pub fn measure(&mut self, qubit: usize, into: usize) -> &mut Self

Measures qubit, storing the outcome in classical bit into.

Source

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.

Source

pub fn z_if(&mut self, bit: usize, q: usize) -> &mut Self

Applies a Pauli-Z on q if classical bit bit is set.

Source

pub fn if_classic(&mut self, bit: usize, op: Op) -> &mut Self

Runs op only if classical bit is set. General classical control.

Source

pub fn validate(&self) -> Result<()>

Checks that every qubit and classical-bit index is in range and that no multi-qubit gate names the same qubit twice.

§Errors

Returns the first Error found, or Ok(()) if the circuit is valid.

Source

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

Emits a valid OpenQASM 3.0 string for this circuit.

§Errors

Returns Error::Qasm if any gate has no OpenQASM 3 name.

Source

pub fn from_qasm(src: &str) -> Result<Self>

Parses an OpenQASM 3.0 string into a Circuit.

§Errors

Returns Error::Qasm on any parse error.

Trait Implementations§

Source§

impl Clone for Circuit

Source§

fn clone(&self) -> Circuit

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Circuit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Circuit

Source§

fn eq(&self, other: &Circuit) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Circuit

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.