use std::fmt;
use serde::{Deserialize, Serialize};
use crate::errors::PauliGraphError;
use crate::{ConditionalBoxData, GateType, Op, gate_type_n_args, gate_type_n_params};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Pauli {
X = 0,
Y = 1,
Z = 2,
I = 3,
}
impl fmt::Display for Pauli {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match self {
Pauli::X => "X",
Pauli::Y => "Y",
Pauli::Z => "Z",
Pauli::I => "I",
};
write!(f, "{s}")
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(try_from = "PauliGraphRaw")]
pub struct PauliGraph {
n_qubits: usize,
ops: Vec<Op>,
}
#[derive(Deserialize)]
struct PauliGraphRaw {
n_qubits: usize,
ops: Vec<Op>,
}
impl TryFrom<PauliGraphRaw> for PauliGraph {
type Error = String;
fn try_from(raw: PauliGraphRaw) -> Result<Self, Self::Error> {
let pg = PauliGraph {
n_qubits: raw.n_qubits,
ops: raw.ops,
};
pg.try_validate().map_err(|e| e.to_string())?;
Ok(pg)
}
}
impl PauliGraph {
pub fn new(n_qubits: usize) -> Self {
Self {
n_qubits,
ops: vec![],
}
}
pub fn with_ops(mut self, ops: Vec<Op>) -> Self {
for op in &ops {
validate_op(op, self.get_n_qubits());
}
self.ops = ops;
self
}
pub fn add_op(&mut self, op: Op) {
validate_op(&op, self.get_n_qubits());
self.ops.push(op);
}
pub fn insert_op(&mut self, index: usize, op: Op) {
validate_op(&op, self.get_n_qubits());
self.ops.insert(index, op);
}
pub fn extend(&mut self, mut other: PauliGraph) {
if self.n_qubits != other.n_qubits {
panic!(
"PauliGraph n_qubits must match: self has {}, other has {}",
self.n_qubits, other.n_qubits
);
}
self.ops.append(&mut other.ops);
}
pub fn add_conditional_op(
&mut self,
op: Op,
conditional_bits: Vec<usize>,
conditional_values: Vec<bool>,
) {
if conditional_bits.len() != conditional_values.len() {
panic!("Length of conditional bits and values must match");
}
if conditional_bits.is_empty() {
self.add_op(op);
} else {
let n_qubits = self.get_n_qubits();
if let Some(last_op) = self.ops.last_mut()
&& let Op::ConditionalBox { data } = last_op
&& conditional_box_matches(data, &conditional_bits, &conditional_values)
{
validate_op(&op, n_qubits);
data.ops.push(op);
return;
}
self.add_op(Op::ConditionalBox {
data: ConditionalBoxData::new(vec![op], conditional_bits, conditional_values),
});
}
}
pub fn insert_conditional_op(
&mut self,
index: usize,
op: Op,
conditional_bits: Vec<usize>,
conditional_values: Vec<bool>,
) {
if conditional_bits.len() != conditional_values.len() {
panic!("Length of conditional bits and values must match");
}
if conditional_bits.is_empty() {
self.insert_op(index, op);
return;
}
let n_qubits = self.get_n_qubits();
if index > 0
&& let Some(Op::ConditionalBox { data }) = self.ops.get_mut(index - 1)
&& conditional_box_matches(data, &conditional_bits, &conditional_values)
{
validate_op(&op, n_qubits);
data.ops.push(op);
return;
}
if let Some(Op::ConditionalBox { data }) = self.ops.get_mut(index)
&& conditional_box_matches(data, &conditional_bits, &conditional_values)
{
validate_op(&op, n_qubits);
data.ops.insert(0, op);
return;
}
self.insert_op(
index,
Op::ConditionalBox {
data: ConditionalBoxData::new(vec![op], conditional_bits, conditional_values),
},
);
}
pub fn remove_op(&mut self, index: usize) {
self.ops.remove(index);
}
pub fn swap_ops(&mut self, index1: usize, index2: usize) {
self.ops.swap(index1, index2);
}
pub fn replace_slice(&mut self, range: std::ops::Range<usize>, new_ops: Vec<Op>) {
for op in &new_ops {
validate_op(op, self.get_n_qubits());
}
self.ops.splice(range, new_ops);
}
pub fn add_qubit(&mut self) {
let old_n_qubits = self.n_qubits;
self.n_qubits += 1;
for op in &mut self.ops {
add_qubit_to_op(op, old_n_qubits);
}
}
pub fn get_ops(&self) -> &Vec<Op> {
&self.ops
}
pub fn get_n_qubits(&self) -> usize {
self.n_qubits
}
pub fn try_validate(&self) -> Result<(), PauliGraphError> {
for op in self.get_ops() {
try_validate_op(op, self.get_n_qubits())?;
}
Ok(())
}
}
fn try_validate_op(op: &Op, pg_nqubits: usize) -> Result<(), PauliGraphError> {
match op {
Op::Rotation { data } => validate_pauli_string_len(data.get_string().len(), pg_nqubits, op),
Op::Measure { data } => validate_pauli_string_len(data.get_string().len(), pg_nqubits, op),
Op::Reset { data } => {
validate_pauli_string_len(data.get_first_string().len(), pg_nqubits, op)?;
validate_pauli_string_len(data.get_second_string().len(), pg_nqubits, op)
}
Op::Tableau { data } => {
for (string, _) in data.get_z_images().iter().chain(data.get_x_images()) {
validate_pauli_string_len(string.len(), pg_nqubits, op)?;
}
Ok(())
}
Op::Gate { data } => {
if let Some(expected_arg_counts) = gate_type_n_args(data.get_gate_type())
&& data.get_args().len() != expected_arg_counts
{
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: format!(
"Gate has wrong number of arguments.\nExpected: {}\nGot: {}",
expected_arg_counts,
data.get_args().len()
),
});
}
let expected_param_counts = gate_type_n_params(data.get_gate_type());
if data.get_params().len() != expected_param_counts {
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: format!(
"Gate has wrong number of parameters.\nExpected: {}\nGot: {}",
expected_param_counts,
data.get_params().len()
),
});
}
if data.get_gate_type() == &GateType::Measure {
validate_op_arg_range(
*data
.get_args()
.first()
.expect("Measure gate missing qubit argument"),
pg_nqubits,
op,
)?;
} else {
validate_op_arg_range(
*data
.get_args()
.iter()
.max()
.expect("Gate missing qubit arguments"),
pg_nqubits,
op,
)?;
}
let conditional_bits_len = data.get_conditional_bits().len();
let conditional_values_len = data.get_conditional_values().len();
if conditional_bits_len != conditional_values_len {
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: format!(
"Gate has mismatched conditional bits and values lengths.\nBits: {}\nValues: {}",
conditional_bits_len, conditional_values_len
),
});
}
if data.get_gate_type() == &GateType::BlackBox {
if data.get_args().is_empty() {
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: "BlackBox gate cannot have empty arguments.".into(),
});
}
if !data.get_conditional_bits().is_empty()
|| !data.get_conditional_values().is_empty()
{
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: "BlackBox gates cannot have conditions.".into(),
});
}
if data.get_data().is_none() {
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: "BlackBox gate is missing payload data.".into(),
});
}
}
Ok(())
}
Op::BlackBox { data } => validate_op_arg_range(
*data
.get_qubits()
.iter()
.max()
.expect("Gate missing qubit argument"),
pg_nqubits,
op,
),
Op::ConditionalBox { data } => {
let conditional_bits_len = data.get_conditional_bits().len();
let conditional_values_len = data.get_conditional_values().len();
if conditional_bits_len != conditional_values_len {
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: format!(
"ConditionalBox has mismatched conditional bits and values lengths.\nBits: {}\nValues: {}",
conditional_bits_len, conditional_values_len
),
});
}
if conditional_bits_len == 0 {
return Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: "ConditionalBox must have non-empty conditional bits and values."
.into(),
});
}
for inner_op in data.get_ops() {
try_validate_op(inner_op, pg_nqubits)?;
}
Ok(())
}
Op::SetBoundary => Ok(()),
}
}
fn validate_op(op: &Op, pg_nqubits: usize) {
try_validate_op(op, pg_nqubits).unwrap_or_else(|e| panic!("{e}"))
}
fn validate_op_arg_range(maxq: usize, n_qubits: usize, op: &Op) -> Result<(), PauliGraphError> {
if maxq >= n_qubits {
Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: format!(
"Operation has argument qubit index out of range.\nExpected: < {}\nGot: {}",
n_qubits, maxq
),
})?
}
Ok(())
}
fn validate_pauli_string_len(len: usize, n_qubits: usize, op: &Op) -> Result<(), PauliGraphError> {
if len != n_qubits {
Err(PauliGraphError::InvalidOp {
op: op.clone(),
message: format!(
"String length does not match graph qubit count.\nExpected: {}\nGot: {}",
n_qubits, len
),
})?
}
Ok(())
}
fn conditional_box_matches(
data: &ConditionalBoxData,
conditional_bits: &[usize],
conditional_values: &[bool],
) -> bool {
data.get_conditional_bits() == conditional_bits
&& data.get_conditional_values() == conditional_values
}
fn append_identity(paulis: &mut Vec<Pauli>) {
paulis.push(Pauli::I);
}
fn single_qubit_tableau_image(n_qubits: usize, pauli: Pauli) -> Vec<Pauli> {
let mut image = vec![Pauli::I; n_qubits];
image.push(pauli);
image
}
fn add_qubit_to_op(op: &mut Op, old_n_qubits: usize) {
match op {
Op::Rotation { data } => {
append_identity(&mut data.string);
}
Op::Measure { data } => {
append_identity(&mut data.string);
}
Op::Reset { data } => {
append_identity(&mut data.first_string);
append_identity(&mut data.second_string);
}
Op::Tableau { data } => {
for (string, _) in &mut data.z_images {
append_identity(string);
}
for (string, _) in &mut data.x_images {
append_identity(string);
}
data.z_images
.push((single_qubit_tableau_image(old_n_qubits, Pauli::Z), false));
data.x_images
.push((single_qubit_tableau_image(old_n_qubits, Pauli::X), false));
}
Op::ConditionalBox { data } => {
for inner_op in &mut data.ops {
add_qubit_to_op(inner_op, old_n_qubits);
}
}
Op::Gate { .. } | Op::BlackBox { .. } | Op::SetBoundary => {}
}
}