Skip to main content

Circuit

Struct Circuit 

Source
pub struct Circuit {
    pub n_qubits: u8,
    pub ops: Vec<Gate>,
}
Expand description

A quantum circuit: n_qubits and an ordered op-log. This is the transportable object — compact, linear in gate count.

Fields§

§n_qubits: u8§ops: Vec<Gate>

Implementations§

Source§

impl Circuit

Source

pub fn new(n_qubits: u8) -> Self

Examples found in repository?
examples/bell.rs (line 10)
9fn main() {
10    let mut c = Circuit::new(2);
11    c.h(0).cx(0, 1); // a Bell pair
12
13    let sv = c.simulate().expect("simulate");
14
15    println!("circuit    {} qubits, {} gates", c.n_qubits, c.ops.len());
16    let (backend, why) = qt::recommend_backend(&c);
17    println!("backend    {backend:?} — {why}");
18
19    println!("outcomes");
20    for (state, p) in qt::top_outcomes(&sv, 4) {
21        if p > 0.0 {
22            println!("  |{state:02b}>  {p:.6}");
23        }
24    }
25
26    // Not "a hash of this run" — the reconstruction is byte-exact, so this is the
27    // same value natively, in a browser, and in a WASI component at the edge.
28    let h = sv.statevector_hash();
29    print!("state-hash ");
30    for b in h {
31        print!("{b:02x}");
32    }
33    println!();
34}
Source

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

Source

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

Source

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

Source

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

Examples found in repository?
examples/bell.rs (line 11)
9fn main() {
10    let mut c = Circuit::new(2);
11    c.h(0).cx(0, 1); // a Bell pair
12
13    let sv = c.simulate().expect("simulate");
14
15    println!("circuit    {} qubits, {} gates", c.n_qubits, c.ops.len());
16    let (backend, why) = qt::recommend_backend(&c);
17    println!("backend    {backend:?} — {why}");
18
19    println!("outcomes");
20    for (state, p) in qt::top_outcomes(&sv, 4) {
21        if p > 0.0 {
22            println!("  |{state:02b}>  {p:.6}");
23        }
24    }
25
26    // Not "a hash of this run" — the reconstruction is byte-exact, so this is the
27    // same value natively, in a browser, and in a WASI component at the edge.
28    let h = sv.statevector_hash();
29    print!("state-hash ");
30    for b in h {
31        print!("{b:02x}");
32    }
33    println!();
34}
Source

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

Source

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

Source

pub fn p(&mut self, k: u16, q: u8) -> &mut Self

P(k) = diag(1, e^{2πi/2^k}) on qubit q.

Source

pub fn cx(&mut self, c: u8, t: u8) -> &mut Self

Examples found in repository?
examples/bell.rs (line 11)
9fn main() {
10    let mut c = Circuit::new(2);
11    c.h(0).cx(0, 1); // a Bell pair
12
13    let sv = c.simulate().expect("simulate");
14
15    println!("circuit    {} qubits, {} gates", c.n_qubits, c.ops.len());
16    let (backend, why) = qt::recommend_backend(&c);
17    println!("backend    {backend:?} — {why}");
18
19    println!("outcomes");
20    for (state, p) in qt::top_outcomes(&sv, 4) {
21        if p > 0.0 {
22            println!("  |{state:02b}>  {p:.6}");
23        }
24    }
25
26    // Not "a hash of this run" — the reconstruction is byte-exact, so this is the
27    // same value natively, in a browser, and in a WASI component at the edge.
28    let h = sv.statevector_hash();
29    print!("state-hash ");
30    for b in h {
31        print!("{b:02x}");
32    }
33    println!();
34}
Source

pub fn cz(&mut self, c: u8, t: u8) -> &mut Self

Source

pub fn cp(&mut self, k: u16, c: u8, t: u8) -> &mut Self

Controlled P(k) — the QFT’s rotation gate.

Source

pub fn ccx(&mut self, c0: u8, c1: u8, t: u8) -> &mut Self

Toffoli (CCX).

Source

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

SWAP(a,b) as three CX (kept in the op-log as gates, not a new opcode).

Source

pub fn qft(n: u8) -> Circuit

The Quantum Fourier Transform over all n qubits — the “quantum FFT”, transported as a circuit (Hadamard + controlled-phase ladder + bit-reversal swaps) and reconstructed byte-exact. Uses the pinned dyadic phases.

Source

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

Validate qubit indices, control/target distinctness, and P(k) range.

Source

pub fn simulate(&self) -> Result<StateVector, QuantumError>

Classically simulate the circuit from |0…0⟩, returning the fixed-point state vector. Pure i64: byte-identical on every machine.

Examples found in repository?
examples/bell.rs (line 13)
9fn main() {
10    let mut c = Circuit::new(2);
11    c.h(0).cx(0, 1); // a Bell pair
12
13    let sv = c.simulate().expect("simulate");
14
15    println!("circuit    {} qubits, {} gates", c.n_qubits, c.ops.len());
16    let (backend, why) = qt::recommend_backend(&c);
17    println!("backend    {backend:?} — {why}");
18
19    println!("outcomes");
20    for (state, p) in qt::top_outcomes(&sv, 4) {
21        if p > 0.0 {
22            println!("  |{state:02b}>  {p:.6}");
23        }
24    }
25
26    // Not "a hash of this run" — the reconstruction is byte-exact, so this is the
27    // same value natively, in a browser, and in a WASI component at the edge.
28    let h = sv.statevector_hash();
29    print!("state-hash ");
30    for b in h {
31        print!("{b:02x}");
32    }
33    println!();
34}
Source

pub fn simulate_from(&self, start: usize) -> Result<StateVector, QuantumError>

Simulate starting from computational basis state |start⟩.

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 Eq for Circuit

Source§

impl PartialEq for Circuit

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.