Skip to content

Commit

Permalink
refactor: rename t2op (#256)
Browse files Browse the repository at this point in the history
For consistency with `Tk2Circuit`
  • Loading branch information
aborgna-q committed Nov 21, 2023
1 parent 0b793be commit 60c6608
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 229 deletions.
2 changes: 1 addition & 1 deletion tket2-py/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn module(py: Python) -> PyResult<&PyModule> {
let m = PyModule::new(py, "_circuit")?;
m.add_class::<Tk2Circuit>()?;
m.add_class::<PyNode>()?;
m.add_class::<tket2::T2Op>()?;
m.add_class::<tket2::Tk2Op>()?;
m.add_class::<tket2::Pauli>()?;

m.add_function(wrap_pyfunction!(validate_hugr, m)?)?;
Expand Down
4 changes: 2 additions & 2 deletions tket2-py/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod chunks;
use std::{cmp::min, convert::TryInto, fs, num::NonZeroUsize, path::PathBuf};

use pyo3::{prelude::*, types::IntoPyDict};
use tket2::{op_matches, passes::apply_greedy_commutation, Circuit, T2Op};
use tket2::{op_matches, passes::apply_greedy_commutation, Circuit, Tk2Op};

use crate::{
circuit::{try_update_hugr, try_with_hugr},
Expand Down Expand Up @@ -111,7 +111,7 @@ fn badger_optimise<'py>(
try_update_hugr(circ, |mut circ, _| {
let n_cx = circ
.commands()
.filter(|c| op_matches(c.optype(), T2Op::CX))
.filter(|c| op_matches(c.optype(), Tk2Op::CX))
.count();
let n_threads = min(
(n_cx / 50).try_into().unwrap_or(1.try_into().unwrap()),
Expand Down
4 changes: 2 additions & 2 deletions tket2/benches/benchmarks/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hugr::extension::prelude::QB_T;
use hugr::extension::PRELUDE_REGISTRY;
use hugr::types::FunctionType;
use hugr::Hugr;
use tket2::T2Op;
use tket2::Tk2Op;

/// Helper function for building circuits.
///
Expand Down Expand Up @@ -47,7 +47,7 @@ pub fn make_cnot_layers(num_qubits: usize, layers: usize) -> Hugr {
let cnot_count = (num_qubits - start) / 2;
for i in 0..cnot_count {
let q = i * 2 + start;
circ.append(T2Op::CX, [q, q + 1])?;
circ.append(Tk2Op::CX, [q, q + 1])?;
}
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tket2/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ mod tests {
};

use super::*;
use crate::{json::load_tk1_json_str, utils::build_simple_circuit, T2Op};
use crate::{json::load_tk1_json_str, utils::build_simple_circuit, Tk2Op};

fn test_circuit() -> Hugr {
load_tk1_json_str(
Expand Down Expand Up @@ -351,7 +351,7 @@ mod tests {
#[test]
fn remove_qubit() {
let mut circ = build_simple_circuit(2, |circ| {
circ.append(T2Op::X, [0])?;
circ.append(Tk2Op::X, [0])?;
Ok(())
})
.unwrap();
Expand Down
18 changes: 9 additions & 9 deletions tket2/src/circuit/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ mod test {
use itertools::Itertools;

use crate::utils::build_simple_circuit;
use crate::T2Op;
use crate::Tk2Op;

use super::*;

Expand All @@ -484,23 +484,23 @@ mod test {
#[test]
fn iterate_commands() {
let circ = build_simple_circuit(2, |circ| {
circ.append(T2Op::H, [0])?;
circ.append(T2Op::CX, [0, 1])?;
circ.append(T2Op::T, [1])?;
circ.append(Tk2Op::H, [0])?;
circ.append(Tk2Op::CX, [0, 1])?;
circ.append(Tk2Op::T, [1])?;
Ok(())
})
.unwrap();

assert_eq!(CommandIterator::new(&circ).count(), 3);

// TODO: Expose the operation names directly in T2Op to clean this up
let t2op_name = |op: T2Op| <T2Op as Into<OpType>>::into(op).name();
// TODO: Expose the operation names directly in Tk2Op to clean this up
let tk2op_name = |op: Tk2Op| <Tk2Op as Into<OpType>>::into(op).name();

let mut commands = CommandIterator::new(&circ);
assert_eq!(commands.size_hint(), (3, Some(3)));

let hadamard = commands.next().unwrap();
assert_eq!(hadamard.optype().name().as_str(), t2op_name(T2Op::H));
assert_eq!(hadamard.optype().name().as_str(), tk2op_name(Tk2Op::H));
assert_eq_iter!(
hadamard.inputs().map(|(u, _, _)| u),
[CircuitUnit::Linear(0)],
Expand All @@ -511,7 +511,7 @@ mod test {
);

let cx = commands.next().unwrap();
assert_eq!(cx.optype().name().as_str(), t2op_name(T2Op::CX));
assert_eq!(cx.optype().name().as_str(), tk2op_name(Tk2Op::CX));
assert_eq_iter!(
cx.inputs().map(|(unit, _, _)| unit),
[CircuitUnit::Linear(0), CircuitUnit::Linear(1)],
Expand All @@ -522,7 +522,7 @@ mod test {
);

let t = commands.next().unwrap();
assert_eq!(t.optype().name().as_str(), t2op_name(T2Op::T));
assert_eq!(t.optype().name().as_str(), tk2op_name(Tk2Op::T));
assert_eq_iter!(
t.inputs().map(|(unit, _, _)| unit),
[CircuitUnit::Linear(1)],
Expand Down
6 changes: 3 additions & 3 deletions tket2/src/circuit/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::num::NonZeroUsize;
use std::ops::{Add, AddAssign};

use crate::ops::op_matches;
use crate::T2Op;
use crate::Tk2Op;

/// The cost for a group of operations in a circuit, each with cost `OpCost`.
pub trait CircuitCost: Add<Output = Self> + Sum<Self> + Debug + Default + Clone + Ord {
Expand Down Expand Up @@ -157,12 +157,12 @@ impl CircuitCost for usize {

/// Returns true if the operation is a controlled X operation.
pub fn is_cx(op: &OpType) -> bool {
op_matches(op, T2Op::CX)
op_matches(op, Tk2Op::CX)
}

/// Returns true if the operation is a quantum operation.
pub fn is_quantum(op: &OpType) -> bool {
let Ok(op): Result<T2Op, _> = op.try_into() else {
let Ok(op): Result<Tk2Op, _> = op.try_into() else {
return false;
};
op.is_quantum()
Expand Down
20 changes: 10 additions & 10 deletions tket2/src/circuit/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,26 @@ mod test {

use crate::json::TKETDecode;
use crate::utils::build_simple_circuit;
use crate::T2Op;
use crate::Tk2Op;

use super::*;

#[test]
fn hash_equality() {
let circ1 = build_simple_circuit(2, |circ| {
circ.append(T2Op::H, [0])?;
circ.append(T2Op::T, [1])?;
circ.append(T2Op::CX, [0, 1])?;
circ.append(Tk2Op::H, [0])?;
circ.append(Tk2Op::T, [1])?;
circ.append(Tk2Op::CX, [0, 1])?;
Ok(())
})
.unwrap();
let hash1 = circ1.circuit_hash().unwrap();

// A circuit built in a different order should have the same hash
let circ2 = build_simple_circuit(2, |circ| {
circ.append(T2Op::T, [1])?;
circ.append(T2Op::H, [0])?;
circ.append(T2Op::CX, [0, 1])?;
circ.append(Tk2Op::T, [1])?;
circ.append(Tk2Op::H, [0])?;
circ.append(Tk2Op::CX, [0, 1])?;
Ok(())
})
.unwrap();
Expand All @@ -170,9 +170,9 @@ mod test {

// Inverting the CX control and target should produce a different hash
let circ3 = build_simple_circuit(2, |circ| {
circ.append(T2Op::T, [1])?;
circ.append(T2Op::H, [0])?;
circ.append(T2Op::CX, [1, 0])?;
circ.append(Tk2Op::T, [1])?;
circ.append(Tk2Op::H, [0])?;
circ.append(Tk2Op::CX, [1, 0])?;
Ok(())
})
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;

use super::json::op::JsonOp;
use crate::ops::load_all_ops;
use crate::T2Op;
use crate::Tk2Op;
use hugr::extension::prelude::PRELUDE;
use hugr::extension::{ExtensionId, ExtensionRegistry, SignatureError};
use hugr::hugr::IdentList;
Expand Down Expand Up @@ -67,7 +67,7 @@ pub static ref LINEAR_BIT: Type = {
.unwrap())
};

/// Extension registry including the prelude, TKET1 and T2Ops extensions.
/// Extension registry including the prelude, TKET1 and Tk2Ops extensions.
pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::from([
TKET1_EXTENSION.clone(),
PRELUDE.clone(),
Expand Down Expand Up @@ -151,7 +151,7 @@ pub static ref SYM_EXPR_T: CustomType =
/// The extension definition for TKET2 ops and types.
pub static ref TKET2_EXTENSION: Extension = {
let mut e = Extension::new(TKET2_EXTENSION_ID);
load_all_ops::<T2Op>(&mut e).expect("add fail");
load_all_ops::<Tk2Op>(&mut e).expect("add fail");

let sym_expr_opdef = e.add_type(
SYM_EXPR_NAME,
Expand Down
4 changes: 2 additions & 2 deletions tket2/src/json/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::circuit::command::{CircuitUnit, Command};
use crate::circuit::Circuit;
use crate::extension::LINEAR_BIT;
use crate::ops::{match_symb_const_op, op_matches};
use crate::T2Op;
use crate::Tk2Op;

use super::op::JsonOp;
use super::{
Expand Down Expand Up @@ -204,7 +204,7 @@ impl JsonEncoder {
// Re-use the parameter from the input.
inputs[0].clone()
}
op if op_matches(op, T2Op::AngleAdd) => {
op if op_matches(op, Tk2Op::AngleAdd) => {
format!("{} + {}", inputs[0], inputs[1])
}
OpType::LeafOp(_) => {
Expand Down
70 changes: 35 additions & 35 deletions tket2/src/json/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use tket_json_rs::optype::OpType as JsonOpType;

use super::OpConvertError;
use crate::extension::{try_unwrap_json_op, LINEAR_BIT};
use crate::T2Op;
use crate::Tk2Op;

/// A serialized operation, containing the operation type and all its attributes.
///
Expand Down Expand Up @@ -177,20 +177,20 @@ impl From<&JsonOp> for OpType {
fn from(json_op: &JsonOp) -> Self {
match json_op.op.op_type {
// JsonOpType::X => LeafOp::X.into(),
JsonOpType::H => T2Op::H.into(),
JsonOpType::CX => T2Op::CX.into(),
JsonOpType::T => T2Op::T.into(),
JsonOpType::Tdg => T2Op::Tdg.into(),
JsonOpType::X => T2Op::X.into(),
JsonOpType::Y => T2Op::Y.into(),
JsonOpType::Z => T2Op::Z.into(),
JsonOpType::Rz => T2Op::RzF64.into(),
JsonOpType::Rx => T2Op::RxF64.into(),
JsonOpType::TK1 => T2Op::TK1.into(),
JsonOpType::PhasedX => T2Op::PhasedX.into(),
JsonOpType::ZZMax => T2Op::ZZMax.into(),
JsonOpType::ZZPhase => T2Op::ZZPhase.into(),
JsonOpType::CZ => T2Op::CZ.into(),
JsonOpType::H => Tk2Op::H.into(),
JsonOpType::CX => Tk2Op::CX.into(),
JsonOpType::T => Tk2Op::T.into(),
JsonOpType::Tdg => Tk2Op::Tdg.into(),
JsonOpType::X => Tk2Op::X.into(),
JsonOpType::Y => Tk2Op::Y.into(),
JsonOpType::Z => Tk2Op::Z.into(),
JsonOpType::Rz => Tk2Op::RzF64.into(),
JsonOpType::Rx => Tk2Op::RxF64.into(),
JsonOpType::TK1 => Tk2Op::TK1.into(),
JsonOpType::PhasedX => Tk2Op::PhasedX.into(),
JsonOpType::ZZMax => Tk2Op::ZZMax.into(),
JsonOpType::ZZPhase => Tk2Op::ZZPhase.into(),
JsonOpType::CZ => Tk2Op::CZ.into(),
JsonOpType::noop => LeafOp::Noop { ty: QB_T }.into(),
_ => LeafOp::CustomOp(Box::new(json_op.as_opaque_op())).into(),
}
Expand All @@ -211,29 +211,29 @@ impl TryFrom<&OpType> for JsonOp {
return Err(err());
};

let json_optype = if let Ok(t2op) = leaf.clone().try_into() {
match t2op {
T2Op::H => JsonOpType::H,
T2Op::CX => JsonOpType::CX,
T2Op::T => JsonOpType::T,
T2Op::S => JsonOpType::S,
T2Op::X => JsonOpType::X,
T2Op::Y => JsonOpType::Y,
T2Op::Z => JsonOpType::Z,
T2Op::Tdg => JsonOpType::Tdg,
T2Op::Sdg => JsonOpType::Sdg,
T2Op::ZZMax => JsonOpType::ZZMax,
T2Op::Measure => JsonOpType::Measure,
T2Op::RzF64 => JsonOpType::Rz,
T2Op::RxF64 => JsonOpType::Rx,
let json_optype = if let Ok(tk2op) = leaf.clone().try_into() {
match tk2op {
Tk2Op::H => JsonOpType::H,
Tk2Op::CX => JsonOpType::CX,
Tk2Op::T => JsonOpType::T,
Tk2Op::S => JsonOpType::S,
Tk2Op::X => JsonOpType::X,
Tk2Op::Y => JsonOpType::Y,
Tk2Op::Z => JsonOpType::Z,
Tk2Op::Tdg => JsonOpType::Tdg,
Tk2Op::Sdg => JsonOpType::Sdg,
Tk2Op::ZZMax => JsonOpType::ZZMax,
Tk2Op::Measure => JsonOpType::Measure,
Tk2Op::RzF64 => JsonOpType::Rz,
Tk2Op::RxF64 => JsonOpType::Rx,
// TODO: Use a TK2 opaque op once we update the tket-json-rs dependency.
T2Op::AngleAdd => {
Tk2Op::AngleAdd => {
unimplemented!("Serialising AngleAdd not supported. Are all constant folded?")
}
T2Op::TK1 => JsonOpType::TK1,
T2Op::PhasedX => JsonOpType::PhasedX,
T2Op::ZZPhase => JsonOpType::ZZPhase,
T2Op::CZ => JsonOpType::CZ,
Tk2Op::TK1 => JsonOpType::TK1,
Tk2Op::PhasedX => JsonOpType::PhasedX,
Tk2Op::ZZPhase => JsonOpType::ZZPhase,
Tk2Op::CZ => JsonOpType::CZ,
}
} else if let LeafOp::CustomOp(b) = leaf {
let ext = (*b).as_ref();
Expand Down
10 changes: 5 additions & 5 deletions tket2/src/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tket_json_rs::optype;
use crate::circuit::Circuit;
use crate::extension::REGISTRY;
use crate::json::TKETDecode;
use crate::T2Op;
use crate::Tk2Op;

const SIMPLE_JSON: &str = r#"{
"phase": "0",
Expand Down Expand Up @@ -94,9 +94,9 @@ fn circ_add_angles_symbolic() -> Hugr {
let f1 = inps.next().unwrap();
let f2 = inps.next().unwrap();

let res = h.add_dataflow_op(T2Op::AngleAdd, [f1, f2]).unwrap();
let res = h.add_dataflow_op(Tk2Op::AngleAdd, [f1, f2]).unwrap();
let f12 = res.outputs().next().unwrap();
let res = h.add_dataflow_op(T2Op::RxF64, [qb, f12]).unwrap();
let res = h.add_dataflow_op(Tk2Op::RxF64, [qb, f12]).unwrap();
let qb = res.outputs().next().unwrap();

h.finish_hugr_with_outputs([qb], &REGISTRY).unwrap()
Expand All @@ -117,12 +117,12 @@ fn circ_add_angles_constants() -> Hugr {
.add_load_const(ConstF64::new(0.3).into(), ExtensionSet::singleton(&f64_ext))
.unwrap();
let point5 = h
.add_dataflow_op(T2Op::AngleAdd, [point2, point3])
.add_dataflow_op(Tk2Op::AngleAdd, [point2, point3])
.unwrap()
.out_wire(0);

let qbs = h
.add_dataflow_op(T2Op::RxF64, [qb, point5])
.add_dataflow_op(Tk2Op::RxF64, [qb, point5])
.unwrap()
.outputs();
h.finish_hugr_with_outputs(qbs, &REGISTRY).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion tket2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ pub mod portmatching;
mod utils;

pub use circuit::Circuit;
pub use ops::{op_matches, symbolic_constant_op, Pauli, T2Op};
pub use ops::{op_matches, symbolic_constant_op, Pauli, Tk2Op};
Loading

0 comments on commit 60c6608

Please sign in to comment.