Skip to content

Commit

Permalink
use prelude types directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Aug 24, 2023
1 parent 554bc79 commit e753079
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde_json = "1.0"
downcast-rs = "1.2.0"
portgraph = "0.7.2"
priority-queue = "1.3.0"
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", branch = "new/tket2-changes" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "937e117" }
smol_str = "0.2.0"
typetag = "0.2.8"
itertools = "0.11.0"
Expand Down
5 changes: 2 additions & 3 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ pub mod command;
//#[cfg(feature = "tkcxx")]
//pub mod unitarybox;

use crate::utils::QB;

use self::command::{Command, CommandIterator};

use hugr::extension::prelude::QB_T;
use hugr::hugr::CircuitUnit;
use hugr::ops::OpTrait;
use hugr::HugrView;
Expand Down Expand Up @@ -48,7 +47,7 @@ pub trait Circuit<'circ>: HugrView {
fn qubits(&self) -> Vec<CircuitUnit> {
self.units()
.iter()
.filter(|(_, typ)| typ == &QB)
.filter(|(_, typ)| typ == &QB_T)
.map(|(unit, _)| *unit)
.collect()
}
Expand Down
7 changes: 4 additions & 3 deletions src/json/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use std::mem;

use hugr::builder::{CircuitBuilder, Container, DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::ExtensionSet;
use hugr::extension::prelude::QB_T;
use hugr::hugr::CircuitUnit;
use hugr::ops::Const;
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::types::FunctionType;
use hugr::{Hugr, Wire};

Expand All @@ -19,7 +21,6 @@ use tket_json_rs::circuit_json::SerialCircuit;
use super::op::JsonOp;
use super::{try_param_to_constant, METADATA_IMPLICIT_PERM, METADATA_PHASE};
use crate::extension::{LINEAR_BIT, TKET1_EXTENSION_ID};
use crate::utils::QB;

/// The state of an in-progress [`DFGBuilder`] being built from a [`SerialCircuit`].
///
Expand Down Expand Up @@ -61,7 +62,7 @@ impl JsonDecoder {
wire_map.insert((register, 0).into(), i);
}
let sig = FunctionType::new_linear(
[vec![QB; num_qubits], vec![LINEAR_BIT.clone(); num_bits]].concat(),
[vec![QB_T; num_qubits], vec![LINEAR_BIT.clone(); num_bits]].concat(),
)
.with_extension_delta(&ExtensionSet::singleton(&TKET1_EXTENSION_ID));

Expand Down Expand Up @@ -139,7 +140,7 @@ impl JsonDecoder {
fn create_param_wire(&mut self, param: &str) -> Wire {
match try_param_to_constant(param) {
Some(c) => {
let const_type = crate::utils::F64;
let const_type = FLOAT64_TYPE;
let const_op = Const::new(c, const_type).unwrap();
self.hugr.add_load_const(const_op).unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions src/json/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::HashMap;

use downcast_rs::Downcast;
use hugr::extension::prelude::QB_T;
use hugr::ops::OpType;
use hugr::std_extensions::arithmetic::float_types::ConstF64;
use hugr::values::{PrimValue, Value};
Expand All @@ -13,7 +14,6 @@ use tket_json_rs::circuit_json::{self, Permutation, SerialCircuit};
use crate::circuit::command::{CircuitUnit, Command};
use crate::circuit::Circuit;
use crate::extension::LINEAR_BIT;
use crate::utils::QB;

use super::op::JsonOp;
use super::{OpConvertError, METADATA_IMPLICIT_PERM, METADATA_PHASE};
Expand Down Expand Up @@ -50,7 +50,7 @@ impl JsonEncoder {
let mut bit_units = HashMap::new();
let mut qubit_units = HashMap::new();
for (unit, ty) in circ.units() {
if ty == QB {
if ty == QB_T {
let index = vec![qubit_units.len() as i64];
let reg = circuit_json::Register("q".to_string(), index);
qubit_units.insert(unit, reg);
Expand Down
16 changes: 9 additions & 7 deletions src/json/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//! explicit count of qubits and linear bits.

use hugr::extension::ExtensionSet;
use hugr::extension::prelude::QB_T;
use hugr::ops::custom::ExternalOp;
use hugr::ops::{LeafOp, OpTrait, OpType};
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::types::FunctionType;

use itertools::Itertools;
Expand All @@ -17,7 +19,7 @@ use tket_json_rs::optype::OpType as JsonOpType;

use super::{try_param_to_constant, OpConvertError};
use crate::extension::{try_unwrap_json_op, LINEAR_BIT, TKET1_EXTENSION_ID};
use crate::utils::{cx_gate, h_gate, F64, QB};
use crate::utils::{cx_gate, h_gate};

/// A serialized operation, containing the operation type and all its attributes.
///
Expand All @@ -35,7 +37,7 @@ pub(crate) struct JsonOp {
/// instead stored purely as metadata for the `Operation`.
param_inputs: Vec<Option<usize>>,
/// The number of non-None inputs in `param_inputs`, corresponding to the
/// F64 inputs to the Hugr operation.
/// FLOAT64_TYPE inputs to the Hugr operation.
num_params: usize,
}

Expand Down Expand Up @@ -123,11 +125,11 @@ impl JsonOp {
#[inline]
pub fn signature(&self) -> FunctionType {
let linear = [
vec![QB; self.num_qubits],
vec![QB_T; self.num_qubits],
vec![LINEAR_BIT.clone(); self.num_bits],
]
.concat();
let params = vec![F64; self.num_params];
let params = vec![FLOAT64_TYPE; self.num_params];
FunctionType::new([linear.clone(), params].concat(), linear)
.with_extension_delta(&ExtensionSet::singleton(&TKET1_EXTENSION_ID))
}
Expand Down Expand Up @@ -181,7 +183,7 @@ impl From<&JsonOp> for OpType {
// JsonOpType::X => LeafOp::X.into(),
JsonOpType::H => h_gate().into(),
JsonOpType::CX => cx_gate().into(),
JsonOpType::noop => LeafOp::Noop { ty: QB }.into(),
JsonOpType::noop => LeafOp::Noop { ty: QB_T }.into(),
// TODO TKET1 measure takes a bit as input, HUGR measure does not
//JsonOpType::Measure => LeafOp::Measure.into(),
// JsonOpType::Reset => LeafOp::Reset.into(),
Expand Down Expand Up @@ -270,11 +272,11 @@ impl TryFrom<&OpType> for JsonOp {
let mut num_bits = 0;
let mut num_params = 0;
for ty in op.signature().input.iter() {
if ty == &QB {
if ty == &QB_T {
num_qubits += 1
} else if *ty == *LINEAR_BIT {
num_bits += 1
} else if ty == &F64 {
} else if ty == &FLOAT64_TYPE {
num_params += 1
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/passes/taso/qtz_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use std::collections::HashMap;

use hugr::builder::{DFGBuilder, Dataflow, DataflowHugr};
use hugr::extension::prelude::QB_T;
// use crate::circuit::{
// circuit::Circuit,
// dag::Edge,
// operation::{Op, WireType},
// };
use hugr::ops::OpType as Op;
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::types::{FunctionType, Type};
use hugr::Hugr as Circuit;
use serde::{Deserialize, Serialize};

use crate::utils::{cx_gate, h_gate, rz_f64, F64, QB};
use crate::utils::{cx_gate, h_gate, rz_f64};

#[derive(Debug, Serialize, Deserialize)]
struct RepCircOp {
Expand Down Expand Up @@ -52,9 +54,9 @@ fn map_op(opstr: &str) -> Op {

fn map_wt(wirestr: &str) -> (Type, usize) {
let wt = if wirestr.starts_with('Q') {
QB
QB_T
} else if wirestr.starts_with('P') {
F64
FLOAT64_TYPE
} else {
panic!("unknown op {wirestr}");
};
Expand All @@ -64,8 +66,8 @@ fn map_wt(wirestr: &str) -> (Type, usize) {
// TODO change to TryFrom
impl From<RepCircData> for Circuit {
fn from(RepCircData { circ: rc, meta }: RepCircData) -> Self {
let qb_types: Vec<Type> = vec![QB; meta.n_qb];
let param_types: Vec<Type> = vec![F64; meta.n_input_param];
let qb_types: Vec<Type> = vec![QB_T; meta.n_qb];
let param_types: Vec<Type> = vec![FLOAT64_TYPE; meta.n_input_param];
let mut circ = DFGBuilder::new(FunctionType::new(
[param_types, qb_types.clone()].concat(),
qb_types,
Expand All @@ -91,9 +93,9 @@ impl From<RepCircData> for Circuit {
.into_iter()
.map(|is| {
let (wt, idx) = map_wt(&is);
if wt == QB {
if wt == QB_T {
qubit_wires[idx]
} else if wt == F64 {
} else if wt == FLOAT64_TYPE {
*param_wires[idx].take().unwrap()
} else {
panic!("unexpected wire type.")
Expand All @@ -104,7 +106,7 @@ impl From<RepCircData> for Circuit {

for (os, wire) in outputs.into_iter().zip(output_wires) {
let (wt, idx) = map_wt(&os);
assert_eq!(wt, QB, "only qubits expected as output");
assert_eq!(wt, QB_T, "only qubits expected as output");

qubit_wires[idx] = wire;
}
Expand Down
5 changes: 3 additions & 2 deletions src/portmatching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub(crate) fn validate_weighted_node<'circ>(

#[cfg(test)]
mod tests {
use hugr::extension::prelude::QB_T;
use hugr::hugr::views::{DescendantsGraph, HierarchyView};
use hugr::ops::handle::DfgID;
use hugr::types::FunctionType;
Expand All @@ -161,12 +162,12 @@ mod tests {
use itertools::Itertools;
use portmatching::PortMatcher;

use crate::utils::{cx_gate, h_gate, QB};
use crate::utils::{cx_gate, h_gate};

use super::{CircuitMatcher, CircuitPattern};

fn h_cx() -> Hugr {
let qb = QB;
let qb = QB_T;
let mut hugr = DFGBuilder::new(FunctionType::new_linear(vec![qb; 2])).unwrap();
let mut circ = hugr.as_circuit(hugr.input_wires().collect());
circ.append(cx_gate(), [0, 1]).unwrap();
Expand Down
5 changes: 0 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//! Utility functions for the library.

use hugr::extension::prelude::QB_T;
use hugr::ops::LeafOp;
use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE;
use hugr::std_extensions::quantum::EXTENSION as QUANTUM;
use hugr::types::Type;
pub(crate) const QB: Type = QB_T;
pub(crate) const F64: Type = FLOAT64_TYPE;

fn get_gate(gate_name: &str) -> LeafOp {
QUANTUM
Expand Down

0 comments on commit e753079

Please sign in to comment.