Struct quil_rs::program::Program

source ·
pub struct Program {
    pub calibrations: Calibrations,
    pub frames: FrameSet,
    pub memory_regions: IndexMap<String, MemoryRegion>,
    pub waveforms: IndexMap<String, Waveform>,
    pub gate_definitions: IndexMap<String, GateDefinition>,
    /* private fields */
}
Expand description

A Quil Program instance describes a quantum program with metadata used in execution.

This contains not only instructions which are executed in turn on the quantum processor, but also the “headers” used to describe and manipulate those instructions, such as calibrations and frame definitions.

Fields§

§calibrations: Calibrations§frames: FrameSet§memory_regions: IndexMap<String, MemoryRegion>§waveforms: IndexMap<String, Waveform>§gate_definitions: IndexMap<String, GateDefinition>

Implementations§

source§

impl Program

source

pub fn new() -> Self

source

pub fn body_instructions(&self) -> impl Iterator<Item = &Instruction>

Returns an iterator over immutable references to the instructions that make up the body of the program.

source

pub fn into_body_instructions(self) -> impl Iterator<Item = Instruction>

source

pub fn clone_without_body_instructions(&self) -> Self

Like Clone, but does not clone the body instructions.

source

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

Add an instruction to the end of the program.

source

pub fn add_instructions<I>(&mut self, instructions: I)
where I: IntoIterator<Item = Instruction>,

source

pub fn filter_instructions( &self, predicate: impl FnMut(&Instruction) -> bool ) -> Program

Return a new Program containing only the instructions for which predicate returns true.

source

pub fn dagger(&self) -> Result<Self, ProgramError>

Creates a new conjugate transpose of the Program by reversing the order of gate instructions and applying the DAGGER modifier to each.

§Errors

Errors if any of the instructions in the program are not Instruction::Gate

source

pub fn expand_calibrations(&self) -> Result<Self, ProgramError>

Expand any instructions in the program which have a matching calibration, leaving the others unchanged. Recurses though each instruction while ensuring there is no cycle in the expansion graph (i.e. no calibration expands directly or indirectly into itself)

source

pub fn from_instructions(instructions: Vec<Instruction>) -> Self

Build a program from a list of instructions

source

pub fn get_frames_for_instruction<'a>( &'a self, instruction: &'a Instruction ) -> Option<MatchedFrames<'a>>

Return the frames which are either “used” or “blocked” by the given instruction.

An instruction “uses” a frame if it plays on that frame; it “blocks” a frame if the instruction prevents other instructions from playing on that frame until complete.

Return None if the instruction does not execute in the context of a frame - such as classical instructions.

See the Quil-T spec for more information.

source

pub fn get_used_qubits(&self) -> &HashSet<Qubit>

Returns a HashSet consisting of every Qubit that is used in the program.

source

pub fn into_instructions(self) -> Vec<Instruction>

Consume the Program to return all of the instructions which constitute it.

source

pub fn into_simplified(&self) -> Result<Self, ProgramError>

Simplify this program into a new Program which contains only instructions and definitions which are executed; effectively, perform dead code removal.

Removes:

  • All calibrations, following calibration expansion
  • Frame definitions which are not used by any instruction such as PULSE or CAPTURE
  • Waveform definitions which are not used by any instruction

When a valid program is simplified, it remains valid.

source

pub fn wrap_in_loop( &self, loop_count_reference: MemoryReference, start_target: Target, end_target: Target, iterations: u32 ) -> Self

Return a copy of the Program wrapped in a loop that repeats iterations times.

The loop is constructed by wrapping the body of the program in classical Quil instructions. The given loop_count_reference must refer to an INTEGER memory region. The value at the reference given will be set to iterations and decremented in the loop. The loop will terminate when the reference reaches 0. For this reason your program should not itself modify the value at the reference unless you intend to modify the remaining number of iterations (i.e. to break the loop).

The given start_target and end_target will be used as the entry and exit points for the loop, respectively. You should provide unique Targets that won’t be used elsewhere in the program.

If iterations is 0, then a copy of the program is returned without any changes.

source

pub fn resolve_placeholders(&mut self)

Resolve [LabelPlaceholder]s and QubitPlaceholders within the program using default resolvers.

See resolve_placeholders_with_custom_resolvers, default_target_resolver, and default_qubit_resolver for more information.

source

pub fn resolve_placeholders_with_custom_resolvers( &mut self, target_resolver: Box<dyn Fn(&TargetPlaceholder) -> Option<String>>, qubit_resolver: Box<dyn Fn(&QubitPlaceholder) -> Option<u64>> )

Resolve TargetPlaceholders and QubitPlaceholders within the program such that the resolved values will remain unique to that placeholder within the scope of the program.

The provided target_resolver and qubit_resolver, will be used to resolve those values respectively. If your placeholder returns None for a particular placeholder, it will not be replaced but will be left as a placeholder.

If you wish to provide a resolver for either labels or qubits, but want to rely on the default behavior for the other, considering using either default_qubit_resolver or default_target_resolver.

source

pub fn default_target_resolver( &self ) -> Box<dyn Fn(&TargetPlaceholder) -> Option<String>>

The default target resolver will resolve each TargetPlaceholder in the program to a unique target by applying an auto-incrementing suffix to the base target.

source

pub fn default_qubit_resolver( &self ) -> Box<dyn Fn(&QubitPlaceholder) -> Option<u64>>

The default qubit resolver will resolve each QubitPlaceholder in the program to a unique fixed qubit index by incrementing to the next available index.

source

pub fn is_empty(&self) -> bool

source

pub fn len(&self) -> usize

source

pub fn to_instructions(&self) -> Vec<Instruction>

Return a copy of all of the instructions which constitute this Program.

source

pub fn to_unitary(&self, n_qubits: u64) -> Result<Matrix, ProgramError>

Return the unitary of a program.

§Errors

Returns an error if the program contains instructions other than Gates.

source

pub fn get_instruction(&self, index: usize) -> Option<&Instruction>

Get a reference to the Instruction at the given index, if present.

Trait Implementations§

source§

impl Add for Program

§

type Output = Program

The resulting type after applying the + operator.
source§

fn add(self, rhs: Program) -> Program

Performs the + operation. Read more
source§

impl AddAssign for Program

source§

fn add_assign(&mut self, rhs: Program)

Performs the += operation. Read more
source§

impl Clone for Program

source§

fn clone(&self) -> Program

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Program

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Program

source§

fn default() -> Program

Returns the “default value” for a type. Read more
source§

impl<'p> From<&'p Program> for ControlFlowGraph<'p>

source§

fn from(value: &'p Program) -> Self

Converts to this type from the input type.
source§

impl From<Vec<Instruction>> for Program

source§

fn from(instructions: Vec<Instruction>) -> Self

Converts to this type from the input type.
source§

impl FromStr for Program

§

type Err = ProgramError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, ProgramError>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for Program

source§

fn eq(&self, other: &Program) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Quil for Program

source§

fn write( &self, writer: &mut impl Write, fall_back_to_debug: bool ) -> Result<(), ToQuilError>

Write the Quil representation of the item to the given writer. If fall_back_to_debug is true, then it must not return an error.
source§

fn to_quil(&self) -> Result<String, ToQuilError>

Return a string in valid Quil syntax or an error if the item cannot be represented with valid Quil.
source§

fn to_quil_or_debug(&self) -> String

Return a string in valid Quil syntax if possible. Any individual component of this object which cannot be represented in Quil will be replaced with a Debug representation of that component.
source§

impl<'a> TryFrom<&'a Program> for BasicBlock<'a>

§

type Error = ProgramEmptyOrContainsMultipleBasicBlocks

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

fn try_from(value: &'a Program) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl StructuralPartialEq for Program

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.