Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove to_headers #184

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 6 additions & 18 deletions quil-py/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl PyProgram {
// declarations and simplify this?
pub fn declarations(&self, py: Python<'_>) -> PyResult<HashMap<String, PyDeclaration>> {
self.as_inner()
.to_instructions(true)
.to_instructions()
.iter()
.filter_map(|inst| match inst {
Instruction::Declaration(declaration) => Some(declaration),
Expand All @@ -106,7 +106,7 @@ impl PyProgram {
#[getter]
pub fn defined_gates(&self, py: Python<'_>) -> PyResult<Vec<PyGateDefinition>> {
self.as_inner()
.to_instructions(true)
.to_instructions()
.iter()
.filter_map(|inst| match inst {
Instruction::GateDefinition(gate_def) => Some(gate_def.to_python(py)),
Expand Down Expand Up @@ -156,28 +156,16 @@ impl PyProgram {
.add_instructions(instructions.into_iter().map(Into::into).collect())
}

pub fn to_instructions(
&self,
include_headers: bool,
py: Python<'_>,
) -> PyResult<Vec<PyInstruction>> {
pub fn to_instructions(&self, py: Python<'_>) -> PyResult<Vec<PyInstruction>> {
self.as_inner()
.to_instructions(include_headers)
.to_instructions()
.iter()
.map(|i| i.to_python(py))
.collect()
}

pub fn to_headers(&self, py: Python<'_>) -> PyResult<Vec<PyInstruction>> {
self.as_inner()
.to_headers()
.iter()
.map(|h| h.to_python(py))
.collect()
}

pub fn __str__(&self) -> String {
self.as_inner().to_string(true)
self.as_inner().to_string()
}

pub fn __add__(&self, py: Python<'_>, rhs: Self) -> PyResult<Self> {
Expand All @@ -197,7 +185,7 @@ impl PyProgram {
// nor be reliably serialized by something like serde using the current
// quil-rs data model.
pub fn __getstate__<'a>(&self, py: Python<'a>) -> &'a PyBytes {
PyBytes::new(py, self.as_inner().to_string(true).as_bytes())
PyBytes::new(py, self.as_inner().to_string().as_bytes())
}

pub fn __setstate__(&mut self, py: Python<'_>, state: &PyBytes) -> PyResult<()> {
Expand Down
8 changes: 5 additions & 3 deletions quil-rs/src/instruction/calibration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::fmt;

use crate::{
instruction::{
format_instructions, format_qubits, get_expression_parameter_string, Expression,
GateModifier, Instruction, Qubit,
format_instructions, get_expression_parameter_string, Expression, GateModifier,
Instruction, Qubit,
},
validation::identifier::{validate_identifier, IdentifierValidationError},
};

use super::format_qubit_parameters;

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Calibration {
pub instructions: Vec<Instruction>,
Expand Down Expand Up @@ -49,7 +51,7 @@ impl fmt::Display for Calibration {
"DEFCAL {}{} {}:",
self.name,
parameter_str,
format_qubits(&self.qubits)
format_qubit_parameters(&self.qubits)
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
)?;
for instruction in &self.instructions {
write!(f, "\n\t{instruction}")?;
Expand Down
14 changes: 13 additions & 1 deletion quil-rs/src/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ pub fn format_qubits(qubits: &[Qubit]) -> String {
.join(" ")
}

/// Format qubits as a Quil parameter list, where each variable qubit must be prefixed with a `%`.
pub fn format_qubit_parameters(qubits: &[Qubit]) -> String {
qubits
.iter()
.map(|q| match q {
Qubit::Fixed(index) => format!("{index}"),
Qubit::Variable(var) => format!("%{var}"),
})
.collect::<Vec<String>>()
.join(" ")
}

MarquessV marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_expression_parameter_string(parameters: &[Expression]) -> String {
if parameters.is_empty() {
return String::new();
Expand Down Expand Up @@ -662,7 +674,7 @@ impl Instruction {
///
///
/// let program = Program::from_str("SHIFT-PHASE 0 \"rf\" 2*2").unwrap();
/// let mut instructions = program.to_instructions(true);
/// let mut instructions = program.to_instructions();
/// instructions.iter_mut().for_each(|inst| inst.apply_to_expressions(Expression::simplify));
///
/// assert_eq!(instructions[0].to_string(), String::from("SHIFT-PHASE 0 \"rf\" 4"))
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 2 additions & 3 deletions quil-rs/src/parser/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,9 @@ mod tests {

for input in inputs {
let program = Program::from_str(input).unwrap();
let output = program.to_string(true);
let output = program.to_string();
let roundtrip = Program::from_str(&output).unwrap();

assert_eq!(program, roundtrip);
assert_eq!(output, roundtrip.to_string());
}
}
}
136 changes: 64 additions & 72 deletions quil-rs/src/program/calibration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,78 +354,70 @@ mod tests {

use crate::program::Program;

#[test]
fn expansion() {
struct TestCase<'a> {
input: &'a str,
expected: &'a str,
}

let cases = vec![
// Test match ordering/precedence
TestCase {
input: concat!(
"DEFCAL RX(%theta) %qubit:\n",
" PULSE 1 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"DEFCAL RX(%theta) 0:\n",
" PULSE 2 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"DEFCAL RX(pi/2) 0:\n",
" PULSE 3 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"RX(pi/2) 1\n",
"RX(pi) 0\n",
"RX(pi/2) 0\n"
),
expected: concat!(
"PULSE 1 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"PULSE 2 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"PULSE 3 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n"
),
},
TestCase {
input: concat!(
"DEFCAL X 0:\n",
" PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"X 0\n"
),
expected: "PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
},
TestCase {
input: concat!(
"DEFCAL X 0:\n",
" Y 0\n",
"DEFCAL Y 0:\n",
" PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"X 0\n"
),
expected: "PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
},
TestCase {
input: concat!(
"DEFCAL MEASURE 0 addr:\n",
" PRAGMA INCORRECT_ORDERING\n",
"DEFCAL MEASURE 0 addr:\n",
" PRAGMA CORRECT\n",
"DEFCAL MEASURE q addr:\n",
" PRAGMA CORRECT\n",
"DEFCAL MEASURE 1 addr:\n",
" PRAGMA INCORRECT_QUBIT\n",
"DEFCAL MEASURE addr:\n",
" PRAGMA INCORRECT_PRECEDENCE\n",
"MEASURE 0 ro\n"
),
expected: "PRAGMA CORRECT\n",
},
TestCase {
input: concat!("DEFCAL I q:\n", " DELAY q 4e-8\n", "I 0\n",),
expected: "DELAY 0 4e-8\n",
},
];

for case in &cases {
let program = Program::from_str(case.input).unwrap();
let calibrated_program = program.expand_calibrations().unwrap();
assert_eq!(calibrated_program.to_string(false).as_str(), case.expected);
}
use insta::assert_snapshot;
use rstest::rstest;

#[rstest]
#[case(
"Calibration-Param-Precedence",
concat!(
"DEFCAL RX(%theta) %qubit:\n",
" PULSE 1 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"DEFCAL RX(%theta) 0:\n",
" PULSE 2 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"DEFCAL RX(pi/2) 0:\n",
" PULSE 3 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"RX(pi/2) 1\n",
"RX(pi) 0\n",
"RX(pi/2) 0\n"
),
)]
#[case(
"Calibration-Simple",
concat!(
"DEFCAL X 0:\n",
" PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"X 0\n",
),
)]
#[case(
"Calibration-Instruction-Match",
concat!(
"DEFCAL X 0:\n",
" Y 0\n",
"DEFCAL Y 0:\n",
" PULSE 0 \"xy\" gaussian(duration: 1, fwhm: 2, t0: 3)\n",
"X 0\n"
),
)]
#[case(
"Measure-Calibration",
concat!(
"DEFCAL MEASURE 0 addr:\n",
" PRAGMA INCORRECT_ORDERING\n",
"DEFCAL MEASURE 0 addr:\n",
" PRAGMA CORRECT\n",
"DEFCAL MEASURE q addr:\n",
" PRAGMA CORRECT\n",
"DEFCAL MEASURE 1 addr:\n",
" PRAGMA INCORRECT_QUBIT\n",
"DEFCAL MEASURE addr:\n",
" PRAGMA INCORRECT_PRECEDENCE\n",
"MEASURE 0 ro\n"
),
)]
#[case(
"Calibration-Variable-Qubit",
concat!("DEFCAL I %q:\n", " DELAY q 4e-8\n", "I 0\n",),
)]
fn test_expansion(#[case] description: &str, #[case] input: &str) {
let program = Program::from_str(input).unwrap();
let calibrated_program = program.expand_calibrations().unwrap();
insta::with_settings!({
snapshot_suffix => description,
}, {
assert_snapshot!(calibrated_program.to_string())
})
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion quil-rs/src/program/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl ScheduledProgram {
let mut working_instructions: Vec<Instruction> = vec![];
let mut blocks = IndexMap::new();

let instructions = program.to_instructions(false);
let instructions = program.to_instructions();

for (index, instruction) in instructions.into_iter().enumerate() {
let instruction_index = Some(index);
Expand Down
Loading