Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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
}

A larger structurally similar circuit, the 5-qubit GHZ state, renders like this (diagram generated by PRISM-Q's own SVG renderer):

GHZ state preparation circuit

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.

Qubit ordering

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.