pub struct DynamicProgramBuilder { /* private fields */ }Expand description
Builds a DynamicProgram from structured control flow.
Instructions append to the block being built. begin_while and
begin_if open a construct that end closes; begin_else switches
the innermost if to its other arm, or reopens an if that end has
just closed. Every index is checked when build validates the program,
so no method here panics on a bad one.
§Examples
Repeat until success: prepare |+>, measure, and retry while the outcome
is 1.
use prism_q::circuit::dynamic::{ClassicalExpr, DynamicProgramBuilder};
use prism_q::{Gate, simulate_program};
let mut b = DynamicProgramBuilder::new(1, 1);
b.add_gate(Gate::H, &[0]).add_measure(0, 0);
b.begin_while("retry", ClassicalExpr::Bit(0));
b.add_reset(0).add_gate(Gate::H, &[0]).add_measure(0, 0);
b.end()?;
let program = b.build()?;
let shots = simulate_program(&program).seed(42).shots(100)?;
assert!(shots.shots.iter().all(|shot| !shot[0]));Implementations§
Source§impl DynamicProgramBuilder
impl DynamicProgramBuilder
pub fn new(num_qubits: usize, num_classical_bits: usize) -> Self
pub fn num_qubits(&self) -> usize
pub fn num_classical_bits(&self) -> usize
Sourcepub fn declare(
&mut self,
name: impl Into<String>,
ty: ClassicalType,
initial: ClassicalValue,
) -> VarId
pub fn declare( &mut self, name: impl Into<String>, ty: ClassicalType, initial: ClassicalValue, ) -> VarId
Declare a variable every shot starts at initial.
Sourcepub fn variable(&self, name: &str) -> Option<VarId>
pub fn variable(&self, name: &str) -> Option<VarId>
The variable declared under name, the latest one when several share it.
Sourcepub fn expr(&self, text: &str) -> Result<ClassicalExpr>
pub fn expr(&self, text: &str) -> Result<ClassicalExpr>
Parse an OpenQASM expression over this builder’s variables, by name, and
its classical bits, read as c[i] for one bit and c for all of them as
an unsigned integer.
§Errors
PrismError::Parse for text that is not an expression, an unknown
name, or a builtin function over a runtime value.
pub fn add_instruction(&mut self, instruction: Instruction) -> &mut Self
pub fn add_gate(&mut self, gate: Gate, targets: &[usize]) -> &mut Self
pub fn add_measure(&mut self, qubit: usize, classical_bit: usize) -> &mut Self
pub fn add_reset(&mut self, qubit: usize) -> &mut Self
Sourcepub fn add_rotation(
&mut self,
kind: RotationKind,
targets: &[usize],
angle: ClassicalExpr,
) -> &mut Self
pub fn add_rotation( &mut self, kind: RotationKind, targets: &[usize], angle: ClassicalExpr, ) -> &mut Self
Apply kind to targets at the angle angle evaluates to when the
block runs.
Sourcepub fn assign(&mut self, var: VarId, value: ClassicalExpr) -> &mut Self
pub fn assign(&mut self, var: VarId, value: ClassicalExpr) -> &mut Self
Store value into var, converted to its declared type.
Sourcepub fn begin_while(&mut self, name: impl Into<String>, condition: ClassicalExpr)
pub fn begin_while(&mut self, name: impl Into<String>, condition: ClassicalExpr)
Open a loop that runs its body while condition is truthy, testing it
before each pass. name labels the loop in the step-limit error.
Sourcepub fn begin_if(&mut self, condition: ClassicalExpr)
pub fn begin_if(&mut self, condition: ClassicalExpr)
Open a branch whose body runs when condition is truthy.
Sourcepub fn begin_else(&mut self) -> Result<()>
pub fn begin_else(&mut self) -> Result<()>
Switch the innermost open if to its else arm, or reopen the if
that Self::end closed last when nothing has been appended since.
§Errors
PrismError::InvalidParameter when there is no such if, or it
already has an else.
Sourcepub fn break_loop(&mut self) -> Result<()>
pub fn break_loop(&mut self) -> Result<()>
Sourcepub fn continue_loop(&mut self) -> Result<()>
pub fn continue_loop(&mut self) -> Result<()>
Skip to the next test of the innermost while condition.
§Errors
PrismError::InvalidParameter outside a loop.
Sourcepub fn build(self) -> Result<DynamicProgram>
pub fn build(self) -> Result<DynamicProgram>
Finish the program, dropping unreachable blocks and merging each straight-line run of blocks into one, so fusion sees the longest circuits the control flow allows.
§Errors
PrismError::InvalidParameter while a while or if is still open,
and anything DynamicProgram::new rejects.
Auto Trait Implementations§
impl Freeze for DynamicProgramBuilder
impl RefUnwindSafe for DynamicProgramBuilder
impl Send for DynamicProgramBuilder
impl Sync for DynamicProgramBuilder
impl Unpin for DynamicProgramBuilder
impl UnsafeUnpin for DynamicProgramBuilder
impl UnwindSafe for DynamicProgramBuilder
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more