Expand description
§qxide — A small experiment expressing qubit ownership with Rust types
Uses the Rust compiler to enforce two rules of quantum mechanics.
- No-cloning theorem: Qubits cannot be copied.
→
Qubitdoes not implementClone/Copy. - Measurement destroys the quantum state: A measured qubit can no longer be used in quantum operations.
→
Circuit::measureconsumesQubitby value (move) and returns a classical bitBit.
As a bonus, Rust’s borrow rules prevent the two-qubit gate cx(&mut a, &mut b) from
using the same qubit as both control and target (cx(&mut q, &mut q) is a compile error).
The built circuit can be sent to two targets.
Circuit::to_qasm: an OpenQASM 3 string commonly accepted by real quantum hardware providersCircuit::simulate: a small state-vector simulator that runs on a regular computer
use qxide::Circuit;
let mut c = Circuit::new();
let mut a = c.qubit();
let mut b = c.qubit();
c.h(&mut a);
c.cx(&mut a, &mut b); // Bell state (entanglement)
let ma = c.measure(a);
let mb = c.measure(b);
let shot = c.simulate(42);
assert_eq!(shot.get(ma), shot.get(mb)); // Entangled qubits always yield the same value§Mistakes prevented by the compiler
Cloning a qubit (violating the no-cloning theorem):
let mut c = qxide::Circuit::new();
let q = c.qubit();
let copy = q.clone();Reusing a measured qubit:
let mut c = qxide::Circuit::new();
let mut q = c.qubit();
let _bit = c.measure(q);
c.h(&mut q);Using the same qubit as both control and target:
let mut c = qxide::Circuit::new();
let mut q = c.qubit();
c.cx(&mut q, &mut q);Cloning a qubit register (QReg also violates the no-cloning theorem):
let mut c = qxide::Circuit::new();
let r = c.qreg(2);
let copy = r.clone();Mutably borrowing the same register twice via indexing:
let mut c = qxide::Circuit::new();
let mut r = c.qreg(2);
c.cx(&mut r[0], &mut r[1]);Borrowing two distinct qubits simultaneously with get_disjoint_mut (duplicate indices are checked at runtime):
let mut c = qxide::Circuit::new();
let mut r = c.qreg(3);
let [a, b] = r.get_disjoint_mut([0, 2]).unwrap();
c.cx(a, b);§Automatic uncomputation
with_ancilla computes a value into an ancilla qubit, allows using it only as a control, and automatically emits the inverse operations. Example that compiles:
let mut c = qxide::Circuit::new();
let mut a = c.qubit();
let mut t = c.qubit();
c.h(&mut a);
c.with_ancilla(
|u, anc| {
u.cx(&a, anc);
u.x(anc);
},
|c, anc| {
c.cx_ctrl(anc, &mut t);
c.cx_ctrl(&a, &mut t);
},
);Using a basis-changing gate (h) not provided by ReversibleCircuit in the compute phase:
let mut c = qxide::Circuit::new();
let mut a = c.qubit();
let mut t = c.qubit();
c.h(&mut a);
c.with_ancilla(
|u, anc| {
u.cx(&a, anc);
u.h(anc);
},
|c, anc| {
c.cx_ctrl(anc, &mut t);
c.cx_ctrl(&a, &mut t);
},
);Mutating the &Qubit ancilla qubit in the apply phase (c.x(anc)):
let mut c = qxide::Circuit::new();
let mut a = c.qubit();
let mut t = c.qubit();
c.h(&mut a);
c.with_ancilla(
|u, anc| {
u.cx(&a, anc);
u.x(anc);
},
|c, anc| {
c.cx_ctrl(anc, &mut t);
c.x(anc);
},
);Mutating an input qubit read in the compute phase during the apply phase (c.h(&mut a), rejected by the borrow checker):
let mut c = qxide::Circuit::new();
let mut a = c.qubit();
let mut t = c.qubit();
c.h(&mut a);
c.with_ancilla(
|u, anc| {
u.cx(&a, anc);
u.x(anc);
},
|c, anc| {
c.cx_ctrl(anc, &mut t);
c.h(&mut a);
},
);Structs§
- Bit
- A classical bit slot that stores a measurement outcome. Because it holds classical information, it can be freely copied.
- BitReg
- A classical bit register that holds measurement results. Because it represents classical information, it can be freely copied.
- Circuit
- A circuit that records quantum operations in sequence.
- QReg
- A register holding multiple qubits. Like
Qubit, it cannot be copied, and it supports indexing and iteration like a slice. - Qubit
- A single qubit in a circuit. Cannot be copied.
- Reversible
Circuit - A restricted circuit builder passed to the compute phase of
Circuit::with_ancillaandCircuit::with_ancillas. Because it exposes onlyx,cx, andccx—which only permute computational basis states—the compute phase maps |x⟩|0⟩ to |x⟩|f(x)⟩, and replaying it in reverse returns all ancilla qubits to |0⟩ exactly.measure,reset, branching, and basis-changing gates (h,rx,ry, etc.) are not provided because they could leave an ancilla entangled witht(e.g.,h(anc), thencx_ctrl(anc, t), thenh(anc)), and diagonal phase gates are also omitted because they cancel out exactly, so such mistakes are caught as compile errors. - Shot
- Measurement results from a single execution.
Enums§
- Backend
- Selects the simulator backend used to execute a circuit in
Circuit::simulate_with. - SimError
- Reason a simulation cannot be executed. Returned instead of silently producing a different result.
Constants§
- DEFAULT_
MEMORY_ LIMIT - Default state-vector memory limit of 1 GiB, accommodating up to 26 qubits.
Functions§
- shot_
seed - Returns the seed passed to the simulator for shot number
shotof a run with seedseed. Mixes both values so runs with nearby seeds (for example--seed 0and--seed 1) share no shots. Used byCircuit::simulate_shotsandqxide run --seed. - state_
vector_ bytes - Memory required for an
n_qubits-qubit state vector (2^n 16-byte complex numbers, 2f64values each), saturating atu128::MAXinstead of overflowing.