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
impl Circuit
Sourcepub fn new(n_qubits: u8) -> Self
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}pub fn x(&mut self, q: u8) -> &mut Self
pub fn y(&mut self, q: u8) -> &mut Self
pub fn z(&mut self, q: u8) -> &mut Self
Sourcepub fn h(&mut self, q: u8) -> &mut Self
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}pub fn s(&mut self, q: u8) -> &mut Self
pub fn t(&mut self, q: u8) -> &mut Self
Sourcepub fn cx(&mut self, c: u8, t: u8) -> &mut Self
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}pub fn cz(&mut self, c: u8, t: u8) -> &mut Self
Sourcepub fn cp(&mut self, k: u16, c: u8, t: u8) -> &mut Self
pub fn cp(&mut self, k: u16, c: u8, t: u8) -> &mut Self
Controlled P(k) — the QFT’s rotation gate.
Sourcepub fn swap(&mut self, a: u8, b: u8) -> &mut Self
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).
Sourcepub fn qft(n: u8) -> Circuit
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.
Sourcepub fn validate(&self) -> Result<(), QuantumError>
pub fn validate(&self) -> Result<(), QuantumError>
Validate qubit indices, control/target distinctness, and P(k) range.
Sourcepub fn simulate(&self) -> Result<StateVector, QuantumError>
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}Sourcepub fn simulate_from(&self, start: usize) -> Result<StateVector, QuantumError>
pub fn simulate_from(&self, start: usize) -> Result<StateVector, QuantumError>
Simulate starting from computational basis state |start⟩.
Trait Implementations§
impl Eq for Circuit
impl StructuralPartialEq for Circuit
Auto Trait Implementations§
impl Freeze for Circuit
impl RefUnwindSafe for Circuit
impl Send for Circuit
impl Sync for Circuit
impl Unpin for Circuit
impl UnsafeUnpin 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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.