Skip to content
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ impl CircuitData {
OperationRef::StandardGate(gate) => gate.into(),
OperationRef::StandardInstruction(instruction) => instruction.into(),
OperationRef::Unitary(unitary) => unitary.clone().into(),
OperationRef::PPM(ppm) => ppm.clone().into(),
};
res.data.push(PackedInstruction {
op: new_op,
Expand All @@ -773,6 +774,7 @@ impl CircuitData {
OperationRef::StandardGate(gate) => gate.into(),
OperationRef::StandardInstruction(instruction) => instruction.into(),
OperationRef::Unitary(unitary) => unitary.clone().into(),
OperationRef::PPM(ppm) => ppm.clone().into(),
};
res.data.push(PackedInstruction {
op: new_op,
Expand Down
48 changes: 44 additions & 4 deletions crates/circuit/src/circuit_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[cfg(feature = "cache_pygates")]
use std::sync::OnceLock;

use numpy::{IntoPyArray, PyArray2, PyReadonlyArray2};
use numpy::{IntoPyArray, PyArray2, PyReadonlyArray1, PyReadonlyArray2};
use pyo3::basic::CompareOp;
use pyo3::exceptions::{PyDeprecationWarning, PyTypeError};
use pyo3::prelude::*;
Expand All @@ -30,8 +30,8 @@ use crate::imports::{
CONTROL_FLOW_OP, CONTROLLED_GATE, GATE, INSTRUCTION, OPERATION, WARNINGS_WARN,
};
use crate::operations::{
ArrayType, Operation, OperationRef, Param, PyGate, PyInstruction, PyOperation, StandardGate,
StandardInstruction, StandardInstructionType, UnitaryGate,
ArrayType, Operation, OperationRef, Param, PauliProductMeasurement, PyGate, PyInstruction,
PyOperation, StandardGate, StandardInstruction, StandardInstructionType, UnitaryGate,
};
use crate::packed_instruction::PackedOperation;
use crate::parameter::parameter_expression::ParameterExpression;
Expand Down Expand Up @@ -190,6 +190,9 @@ impl CircuitInstruction {
OperationRef::Unitary(unitary) => unitary
.create_py_op(py, self.label.as_ref().map(|x| x.as_str()))?
.into_any(),
OperationRef::PPM(ppm) => ppm
.create_py_op(py, self.label.as_ref().map(|x| x.as_str()))?
.into_any(),
};

#[cfg(feature = "cache_pygates")]
Expand Down Expand Up @@ -580,7 +583,8 @@ impl<'py> FromPyObject<'py> for OperationFromPython {
}

// We need to check by name here to avoid a circular import during initial loading
if ob.getattr(intern!(py, "name"))?.extract::<String>()? == "unitary" {
let ob_name = ob.getattr(intern!(py, "name"))?.extract::<String>()?;
if ob_name == "unitary" {
let params = extract_params()?;
if let Some(Param::Obj(data)) = params.first() {
let py_matrix: PyReadonlyArray2<Complex64> = data.extract(py)?;
Expand Down Expand Up @@ -616,6 +620,42 @@ impl<'py> FromPyObject<'py> for OperationFromPython {
});
};
}
} else if ob_name == "PauliProductMeasurement" {
let params = extract_params_no_coerce()?;

let z = if let Param::Obj(z) = &params[0] {
z.extract::<PyReadonlyArray1<bool>>(py)?
.as_slice()?
.to_vec()
} else {
return Err(PyTypeError::new_err(format!("invalid input: {ob}")));
};

let x = if let Param::Obj(x) = &params[1] {
x.extract::<PyReadonlyArray1<bool>>(py)?
.as_slice()?
.to_vec()
} else {
return Err(PyTypeError::new_err(format!("invalid input: {ob}")));
};

let phase = if let Param::Obj(phase) = &params[2] {
phase.extract::<u8>(py)?
} else {
return Err(PyTypeError::new_err(format!("invalid input: {ob}")));
};

let pauli_product_measurement = Box::new(PauliProductMeasurement {
z: z.to_owned(),
x: x.to_owned(),
phase,
});

return Ok(OperationFromPython {
operation: PackedOperation::from_ppm(pauli_product_measurement),
params: SmallVec::new(),
label: extract_label()?,
});
}

if ob_type.is_subclass(GATE.get_bound(py))? {
Expand Down
1 change: 1 addition & 0 deletions crates/circuit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub fn dag_to_circuit(dag: &DAGCircuit, copy_operations: bool) -> PyResult<Circu
OperationRef::StandardGate(gate) => gate.into(),
OperationRef::StandardInstruction(instruction) => instruction.into(),
OperationRef::Unitary(unitary) => unitary.clone().into(),
OperationRef::PPM(ppm) => ppm.clone().into(),
};
Ok(PackedInstruction {
op,
Expand Down
2 changes: 2 additions & 0 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,7 @@ impl DAGCircuit {
_ => Ok(false),
}
}
[OperationRef::PPM(op_a), OperationRef::PPM(op_b)] => Ok(op_a == op_b),
_ => Ok(false),
}
}
Expand Down Expand Up @@ -7210,6 +7211,7 @@ impl DAGCircuit {
OperationRef::StandardGate(gate) => gate.into(),
OperationRef::StandardInstruction(instruction) => instruction.into(),
OperationRef::Unitary(unitary) => unitary.clone().into(),
OperationRef::PPM(ppm) => ppm.clone().into(),
}
} else {
instr.op.clone()
Expand Down
2 changes: 2 additions & 0 deletions crates/circuit/src/dag_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ impl DAGOpNode {
OperationRef::StandardGate(gate) => gate.into(),
OperationRef::StandardInstruction(instruction) => instruction.into(),
OperationRef::Unitary(unitary) => unitary.clone().into(),
OperationRef::PPM(ppm) => ppm.clone().into(),
};
#[cfg(feature = "cache_pygates")]
{
Expand Down Expand Up @@ -282,6 +283,7 @@ impl DAGOpNode {
OperationRef::StandardGate(gate) => gate.into(),
OperationRef::StandardInstruction(instruction) => instruction.into(),
OperationRef::Unitary(unitary) => unitary.clone().into(),
OperationRef::PPM(ppm) => ppm.clone().into(),
}
} else {
self.instruction.operation.clone()
Expand Down
4 changes: 4 additions & 0 deletions crates/circuit/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub static UNITARY_GATE: ImportOnceCell = ImportOnceCell::new(
"qiskit.circuit.library.generalized_gates.unitary",
"UnitaryGate",
);
pub static PAULI_PRODUCT_MEASUREMENT: ImportOnceCell = ImportOnceCell::new(
"qiskit.circuit.library.pauli_product_measurement",
"PauliProductMeasurement",
);
pub static MCPHASE_GATE: ImportOnceCell =
ImportOnceCell::new("qiskit.circuit.library", "MCPhaseGate");
pub static PAULI_EVOLUTION_GATE: ImportOnceCell =
Expand Down
94 changes: 93 additions & 1 deletion crates/circuit/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use std::sync::Arc;
use std::{fmt, vec};

use crate::circuit_data::CircuitData;
use crate::imports::{BARRIER, DELAY, MEASURE, RESET, get_std_gate_class};
use crate::imports::{
BARRIER, DELAY, MEASURE, PAULI_PRODUCT_MEASUREMENT, RESET, get_std_gate_class,
};
use crate::imports::{DEEPCOPY, QUANTUM_CIRCUIT, UNITARY_GATE};
use crate::parameter::parameter_expression::{
ParameterExpression, PyParameter, PyParameterExpression,
Expand Down Expand Up @@ -275,6 +277,7 @@ pub enum OperationRef<'a> {
Instruction(&'a PyInstruction),
Operation(&'a PyOperation),
Unitary(&'a UnitaryGate),
PPM(&'a PauliProductMeasurement),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we spell PPM out? No other gates are abbreviated and it'll be more consistent with PauliEvolutionGate or PauliRotationGate.

}

impl Operation for OperationRef<'_> {
Expand All @@ -287,6 +290,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.name(),
Self::Operation(operation) => operation.name(),
Self::Unitary(unitary) => unitary.name(),
Self::PPM(ppm) => ppm.name(),
}
}
#[inline]
Expand All @@ -298,6 +302,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.num_qubits(),
Self::Operation(operation) => operation.num_qubits(),
Self::Unitary(unitary) => unitary.num_qubits(),
Self::PPM(ppm) => ppm.num_qubits(),
}
}
#[inline]
Expand All @@ -309,6 +314,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.num_clbits(),
Self::Operation(operation) => operation.num_clbits(),
Self::Unitary(unitary) => unitary.num_clbits(),
Self::PPM(ppm) => ppm.num_clbits(),
}
}
#[inline]
Expand All @@ -320,6 +326,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.num_params(),
Self::Operation(operation) => operation.num_params(),
Self::Unitary(unitary) => unitary.num_params(),
Self::PPM(ppm) => ppm.num_params(),
}
}
#[inline]
Expand All @@ -331,6 +338,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.control_flow(),
Self::Operation(operation) => operation.control_flow(),
Self::Unitary(unitary) => unitary.control_flow(),
Self::PPM(ppm) => ppm.control_flow(),
}
}
#[inline]
Expand All @@ -342,6 +350,7 @@ impl Operation for OperationRef<'_> {
OperationRef::Instruction(instruction) => instruction.blocks(),
OperationRef::Operation(operation) => operation.blocks(),
Self::Unitary(unitary) => unitary.blocks(),
Self::PPM(ppm) => ppm.blocks(),
}
}
#[inline]
Expand All @@ -353,6 +362,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.matrix(params),
Self::Operation(operation) => operation.matrix(params),
Self::Unitary(unitary) => unitary.matrix(params),
Self::PPM(ppm) => ppm.matrix(params),
}
}
#[inline]
Expand All @@ -364,6 +374,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.definition(params),
Self::Operation(operation) => operation.definition(params),
Self::Unitary(unitary) => unitary.definition(params),
Self::PPM(ppm) => ppm.definition(params),
}
}
#[inline]
Expand All @@ -375,6 +386,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.directive(),
Self::Operation(operation) => operation.directive(),
Self::Unitary(unitary) => unitary.directive(),
Self::PPM(ppm) => ppm.directive(),
}
}

Expand All @@ -388,6 +400,7 @@ impl Operation for OperationRef<'_> {
Self::Instruction(instruction) => instruction.matrix_as_static_1q(params),
Self::Operation(operation) => operation.matrix_as_static_1q(params),
Self::Unitary(unitary) => unitary.matrix_as_static_1q(params),
Self::PPM(ppm) => ppm.matrix_as_static_1q(params),
}
}
}
Expand Down Expand Up @@ -2890,3 +2903,82 @@ impl UnitaryGate {
}
}
}

/// This class represents a PauliProductMeasurement instruction.
#[derive(Clone, Debug)]
#[repr(align(8))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current format I don't think we need this, since bool and u8 are both a byte long. But if we change to bitvec or some bitpacking we might need to keep it?

pub struct PauliProductMeasurement {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to construct the PPMs in the Rust Litinski transformation code, which currently outputs the Pauli in a sparse string format (sign, Paulistring, indices). If we use ZX bits as internal representation we'll also need a converter from Pauli string to ZX in Rust.

/// The z-component of the pauli.
pub z: Vec<bool>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there alternatives to storing boolean vectors, i.e. 1 byte per term if we'd only need 1 bit? E.g. the bitvec package seems to have a convenient interface. I assume @jakelishman has some opinions here 🙂

/// The x-component of the pauli.
pub x: Vec<bool>,
/// The phase of the pauli. This is an integer modulo 4.
pub phase: u8,
}

impl Operation for PauliProductMeasurement {
fn name(&self) -> &str {
"PauliProductMeasurement"
}
fn num_qubits(&self) -> u32 {
self.z.len() as u32
}
fn num_clbits(&self) -> u32 {
1
}
fn num_params(&self) -> u32 {
0
}
fn control_flow(&self) -> bool {
false
}
fn blocks(&self) -> Vec<CircuitData> {
vec![]
}
fn matrix(&self, _params: &[Param]) -> Option<Array2<Complex64>> {
None
}
fn definition(&self, _params: &[Param]) -> Option<CircuitData> {
// Similarly to UnitaryGate, we do not provide the actual decomposition here.
// Instead, the HighLevelSynthesis transpiler pass is modified to call the
// relevant synthesis function when requiring the definition for a
// PauliProudctMeasurement.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// PauliProudctMeasurement.
// PauliProductMeasurement.

None
}
fn directive(&self) -> bool {
false
}
fn matrix_as_static_1q(&self, _params: &[Param]) -> Option<[[Complex64; 2]; 2]> {
None
}

fn matrix_as_nalgebra_1q(&self, _params: &[Param]) -> Option<Matrix2<Complex64>> {
None
}
}

impl PauliProductMeasurement {
pub fn create_py_op(&self, py: Python, label: Option<&str>) -> PyResult<Py<PyAny>> {
let z = PyList::new(py, &self.z)?;
let x = PyList::new(py, &self.x)?;
let phase = self.phase;
let py_label = if let Some(label) = label {
label.into_py_any(py)?
} else {
py.None()
};

let gate = PAULI_PRODUCT_MEASUREMENT
.get_bound(py)
.call_method1(intern!(py, "_from_pauli_data"), (z, x, phase, py_label))?;
Ok(gate.unbind())
}
}

impl PartialEq for PauliProductMeasurement {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.z == other.z && self.phase == other.phase
}
}

impl Eq for PauliProductMeasurement {}
Loading