Skip to main content

DynamicProgramBuilder

Struct DynamicProgramBuilder 

Source
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

Source

pub fn new(num_qubits: usize, num_classical_bits: usize) -> Self

Source

pub fn num_qubits(&self) -> usize

Source

pub fn num_classical_bits(&self) -> usize

Source

pub fn declare( &mut self, name: impl Into<String>, ty: ClassicalType, initial: ClassicalValue, ) -> VarId

Declare a variable every shot starts at initial.

Source

pub fn variable(&self, name: &str) -> Option<VarId>

The variable declared under name, the latest one when several share it.

Source

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.

Source

pub fn add_instruction(&mut self, instruction: Instruction) -> &mut Self

Source

pub fn add_gate(&mut self, gate: Gate, targets: &[usize]) -> &mut Self

Source

pub fn add_measure(&mut self, qubit: usize, classical_bit: usize) -> &mut Self

Source

pub fn add_reset(&mut self, qubit: usize) -> &mut Self

Source

pub fn append(&mut self, circuit: &Circuit) -> &mut Self

Append every instruction of circuit.

Source

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.

Source

pub fn assign(&mut self, var: VarId, value: ClassicalExpr) -> &mut Self

Store value into var, converted to its declared type.

Source

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.

Source

pub fn begin_if(&mut self, condition: ClassicalExpr)

Open a branch whose body runs when condition is truthy.

Source

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.

Source

pub fn end(&mut self) -> Result<()>

Close the innermost while or if.

§Errors

PrismError::InvalidParameter when nothing is open.

Source

pub fn break_loop(&mut self) -> Result<()>

Leave the innermost while.

§Errors

PrismError::InvalidParameter outside a loop.

Source

pub fn continue_loop(&mut self) -> Result<()>

Skip to the next test of the innermost while condition.

§Errors

PrismError::InvalidParameter outside a loop.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V