Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion quil-rs/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ use std::collections::{HashMap, HashSet};
use std::ops;
use std::str::FromStr;

use analysis::ControlFlowGraph;
use indexmap::{IndexMap, IndexSet};
use ndarray::Array2;
use nom_locate::LocatedSpan;
use scheduling::Seconds;

use crate::instruction::{
Arithmetic, ArithmeticOperand, ArithmeticOperator, Declaration, FrameDefinition,
FrameIdentifier, GateDefinition, GateError, Instruction, Jump, JumpUnless, Label, Matrix,
FrameIdentifier, Gate, GateDefinition, GateError, Instruction, Jump, JumpUnless, Label, Matrix,
MemoryReference, Move, Qubit, QubitPlaceholder, ScalarType, Target, TargetPlaceholder, Vector,
Waveform, WaveformDefinition,
};
Expand Down Expand Up @@ -131,6 +133,31 @@ impl Program {
}
}

pub fn get_gates_duration(&self) -> Result<HashMap<Gate, Seconds>> {
Ok(self
.instructions
.iter()
.filter_map(|ins| match ins {
Instruction::Gate(gate) => Some(gate),
_ => None,
})
.filter_map(|gate| {
let cal = self.calibrations.get_match_for_gate(gate)?;
let p = Program::from_instructions(cal.instructions.to_owned());
let cfg = ControlFlowGraph::from(&p);
let blocks = cfg.into_blocks();
let block = blocks.first()?;
// TODO: handle error
// NOTE: we need full program to expand calibrations
let duration = block.as_schedule_seconds(self).ok()?.duration().to_owned();

let gate = gate.to_owned();

Some((gate, duration))
})
.collect::<HashMap<_, _>>())
}

/// Add an instruction to the end of the program.
pub fn add_instruction(&mut self, instruction: Instruction) {
self.used_qubits
Expand Down