Your First Circuit
There are two ways to build a circuit: the fluent CircuitBuilder API, or by parsing
OpenQASM text.
With the builder
CircuitBuilder chains gate calls and runs the result. This builds a Bell pair, the
two-qubit entangled state (|00⟩ + |11⟩) / √2:
#![allow(unused)] fn main() { use prism_q::CircuitBuilder; let result = CircuitBuilder::new(2) .h(0) .cx(0, 1) .run(42) // seed = 42 .expect("simulation failed"); let probs = result.probabilities.expect("no probabilities"); for i in 0..probs.len() { let p = probs.get(i); if p > 1e-10 { println!("|{i:02b}> = {p:.4}"); } } // |00> = 0.5000 // |11> = 0.5000 }
Measurement is Z-basis by default. measure_in_basis(qubit, axis, bit) measures along
X, Y, or Z, and measure_pauli_product(&terms, bit) records the parity of a Pauli
string such as X0 Z1. Both lower onto the gates the backends already run, so every
backend accepts them. A basis measurement leaves the qubit in the Z eigenstate of the
recorded bit rather than rotating back, and a Pauli product accumulates its parity on one
extra qubit appended past the declared register:
#![allow(unused)] fn main() { use prism_q::{CircuitBuilder, PauliAxis, PauliTerm}; let circuit = CircuitBuilder::new_with_classical(2, 2) .h(0) .cx(0, 1) .measure_in_basis(0, PauliAxis::X, 0) .measure_pauli_product(&[PauliTerm::z(0), PauliTerm::z(1)], 1) .build(); assert_eq!(circuit.num_qubits, 3); }
A larger structurally similar circuit, the 5-qubit GHZ state, renders like this (diagram generated by PRISM-Q's own SVG renderer):
From OpenQASM
The same Bell pair, written in OpenQASM 3.0 and parsed:
#![allow(unused)] fn main() { use prism_q::circuit::openqasm; use prism_q::simulate; let qasm = r#" OPENQASM 3.0; include "stdgates.inc"; qubit[2] q; h q[0]; cx q[0], q[1]; "#; let circuit = openqasm::parse(qasm).expect("failed to parse QASM"); let result = simulate(&circuit).seed(42).run().expect("simulation failed"); }
run_qasm(qasm, seed) is a shortcut that parses and simulates in one call. See the
OpenQASM Support guide for the supported subset.
q[0] is the least significant bit, so x q[0] produces state index 1, not 2. Bitstrings
print most-significant qubit first.
Next: sample measurement outcomes in Shots and Sampling.