Skip to content

Commit

Permalink
chore: Update devenv + bump hugr to latest (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Dec 8, 2023
1 parent cf1dd85 commit 9ef975e
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 244 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ missing_docs = "warn"
[workspace.dependencies]

tket2 = { path = "./tket2" }
quantinuum-hugr = { git = "https://github.com/CQCL/hugr", rev = "e7473f2" }
quantinuum-hugr = { git = "https://github.com/CQCL/hugr", rev = "2efcfb3" }
portgraph = { version = "0.10" }
pyo3 = { version = "0.20" }
itertools = { version = "0.12.0" }
Expand Down
36 changes: 18 additions & 18 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1699492209,
"narHash": "sha256-AhaFZrKIpU6GYUaA26erOQg2X+YHHzJpJ8r1mBHOaM8=",
"lastModified": 1701187605,
"narHash": "sha256-NctguPdUeDVLXFsv6vI1RlEiHLsXkeW3pgZe/mwn1BU=",
"owner": "cachix",
"repo": "devenv",
"rev": "80e740c7eb91b3d1c82013ec0ba4bfbc9a83734a",
"rev": "a7c4dd8f4eb1f98a6b8f04bf08364954e1e73e4f",
"type": "github"
},
"original": {
Expand All @@ -25,11 +25,11 @@
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1699597299,
"narHash": "sha256-uJMCDTKSUB7+K+s7SB2DS6WU2VGDmruXmP9TQwTYGkw=",
"lastModified": 1701325357,
"narHash": "sha256-+CF74n9/AlLwgdCTM5WuKsa/4C1YxJSpRDCfz1ErOl0=",
"owner": "nix-community",
"repo": "fenix",
"rev": "ae8ecab0dbfe3552bd1a0bf5504416fd07dd2e8a",
"rev": "07a409ce1fe2c6d6e871793394b0cc0e5e262e3b",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -95,11 +95,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1699343069,
"narHash": "sha256-s7BBhyLA6MI6FuJgs4F/SgpntHBzz40/qV0xLPW6A1Q=",
"lastModified": 1701237617,
"narHash": "sha256-Ryd8xpNDY9MJnBFDYhB37XSFIxCPVVVXAbInNPa95vs=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "ec750fd01963ab6b20ee1f0cb488754e8036d89d",
"rev": "85306ef2470ba705c97ce72741d56e42d0264015",
"type": "github"
},
"original": {
Expand All @@ -111,11 +111,11 @@
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1699291058,
"narHash": "sha256-5ggduoaAMPHUy4riL+OrlAZE14Kh7JWX4oLEs22ZqfU=",
"lastModified": 1701053011,
"narHash": "sha256-8QQ7rFbKFqgKgLoaXVJRh7Ik5LtI3pyBBCfOnNOGkF0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "41de143fda10e33be0f47eab2bfe08a50f234267",
"rev": "5b528f99f73c4fad127118a8c1126b5e003b01a9",
"type": "github"
},
"original": {
Expand Down Expand Up @@ -152,11 +152,11 @@
"nixpkgs-stable": "nixpkgs-stable_2"
},
"locked": {
"lastModified": 1699271226,
"narHash": "sha256-8Jt1KW3xTjolD6c6OjJm9USx/jmL+VVmbooADCkdDfU=",
"lastModified": 1700922917,
"narHash": "sha256-ej2fch/T584b5K9sk1UhmZF7W6wEfDHuoUYpFN8dtvM=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "ea758da1a6dcde6dc36db348ed690d09b9864128",
"rev": "e5ee5c5f3844550c01d2131096c7271cec5e9b78",
"type": "github"
},
"original": {
Expand All @@ -177,11 +177,11 @@
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1699451299,
"narHash": "sha256-7HJMyp62fCS6/aCCCASz8MdJM2/M8d1pBNukyLmPdwA=",
"lastModified": 1701186284,
"narHash": "sha256-euPBY3EmEy7+Jjm2ToRPlSp/qrj0UL9+PRobxVz6+aQ=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "7059ae2fc2d55fa20d7e2671597b516431129445",
"rev": "c7c582afb57bb802715262d7f1ba73b8a86c1c5a",
"type": "github"
},
"original": {
Expand Down
2 changes: 1 addition & 1 deletion devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ in

languages.rust = {
enable = true;
channel = "stable";
channel = "beta";
components = [ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" ];
};

Expand Down
10 changes: 6 additions & 4 deletions tket2/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use portgraph::Direction;
use thiserror::Error;

pub use hugr::ops::OpType;
pub use hugr::types::{EdgeKind, Signature, Type, TypeRow};
pub use hugr::types::{EdgeKind, Type, TypeRow};
pub use hugr::{Node, Port, Wire};

use self::units::{filter, FilteredUnits, Units};
Expand All @@ -46,9 +46,11 @@ pub trait Circuit: HugrView {
///
/// Equivalent to [`HugrView::get_function_type`].
#[inline]
fn circuit_signature(&self) -> &FunctionType {
fn circuit_signature(&self) -> FunctionType {
self.get_function_type()
.expect("Circuit has no function type")
.body()
.clone()
}

/// Returns the input node to the circuit.
Expand Down Expand Up @@ -338,8 +340,8 @@ mod tests {
let circ = test_circuit();

assert_eq!(circ.name(), None);
assert_eq!(circ.circuit_signature().input.len(), 3);
assert_eq!(circ.circuit_signature().output.len(), 3);
assert_eq!(circ.circuit_signature().input_count(), 3);
assert_eq!(circ.circuit_signature().output_count(), 3);
assert_eq!(circ.qubit_count(), 2);
assert_eq!(circ.num_gates(), 3);

Expand Down
2 changes: 1 addition & 1 deletion tket2/src/circuit/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::units::{filter, DefaultUnitLabeller, LinearUnit, UnitLabeller, Units}
use super::Circuit;

pub use hugr::ops::OpType;
pub use hugr::types::{EdgeKind, Signature, Type, TypeRow};
pub use hugr::types::{EdgeKind, Type, TypeRow};
pub use hugr::{CircuitUnit, Direction, Node, Port, PortIndex, Wire};

/// An operation applied to specific wires.
Expand Down
62 changes: 34 additions & 28 deletions tket2/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
//!
//! This includes a extension for the opaque TKET1 operations.
use std::collections::HashMap;

use super::json::op::JsonOp;
use crate::ops::load_all_ops;
use crate::Tk2Op;
use hugr::extension::prelude::PRELUDE;
use hugr::extension::{ExtensionId, ExtensionRegistry, SignatureError};
use hugr::extension::simple_op::MakeOpDef;
use hugr::extension::{CustomSignatureFunc, ExtensionId, ExtensionRegistry, SignatureError};
use hugr::hugr::IdentList;
use hugr::ops::custom::{ExternalOp, OpaqueOp};
use hugr::std_extensions::arithmetic::float_types::{extension as float_extension, FLOAT64_TYPE};
use hugr::types::type_param::{CustomTypeArg, TypeArg, TypeParam};
use hugr::types::{CustomType, FunctionType, Type, TypeBound};
use hugr::types::{CustomType, FunctionType, PolyFuncType, Type, TypeBound};
use hugr::{type_row, Extension};
use lazy_static::lazy_static;
use smol_str::SmolStr;
Expand Down Expand Up @@ -45,14 +43,11 @@ pub static ref TKET1_EXTENSION: Extension = {
res.add_type(LINEAR_BIT_NAME, vec![], "A linear bit.".into(), TypeBound::Any.into()).unwrap();

let json_op_payload_def = res.add_type(JSON_PAYLOAD_NAME, vec![], "Opaque TKET1 operation metadata.".into(), TypeBound::Eq.into()).unwrap();
let json_op_payload = TypeParam::Opaque(json_op_payload_def.instantiate([]).unwrap());
res.add_op_custom_sig(
let json_op_payload = TypeParam::Opaque{ty:json_op_payload_def.instantiate([]).unwrap()};
res.add_op(
JSON_OP_NAME,
"An opaque TKET1 operation.".into(),
vec![json_op_payload],
HashMap::new(),
vec![],
json_op_signature,
JsonOpSignature([json_op_payload])
).unwrap();

res
Expand All @@ -68,12 +63,12 @@ pub static ref LINEAR_BIT: Type = {
};

/// Extension registry including the prelude, TKET1 and Tk2Ops extensions.
pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::from([
pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::try_new([
TKET1_EXTENSION.clone(),
PRELUDE.clone(),
TKET2_EXTENSION.clone(),
float_extension(),
]);
]).unwrap();


}
Expand All @@ -98,7 +93,7 @@ pub(crate) fn wrap_json_op(op: &JsonOp) -> ExternalOp {
JSON_OP_NAME,
"".into(),
vec![payload],
Some(sig),
sig,
)
.into()
}
Expand All @@ -111,22 +106,34 @@ pub(crate) fn try_unwrap_json_op(ext: &ExternalOp) -> Option<JsonOp> {
if ext.name() != format!("{TKET1_EXTENSION_ID}.{JSON_OP_NAME}") {
return None;
}
let Some(TypeArg::Opaque { arg }) = ext.args().get(0) else {
let Some(TypeArg::Opaque { arg }) = ext.args().first() else {
// TODO: Throw an error? We should never get here if the name matches.
return None;
};
let op = serde_yaml::from_value(arg.value.clone()).ok()?;
Some(op)
}

/// Compute the signature of a json-encoded TKET1 operation.
fn json_op_signature(args: &[TypeArg]) -> Result<FunctionType, SignatureError> {
let [TypeArg::Opaque { arg }] = args else {
// This should have already been checked.
panic!("Wrong number of arguments");
};
let op: JsonOp = serde_yaml::from_value(arg.value.clone()).unwrap(); // TODO Errors!
Ok(op.signature())
struct JsonOpSignature([TypeParam; 1]);

impl CustomSignatureFunc for JsonOpSignature {
fn compute_signature<'o, 'a: 'o>(
&'a self,
arg_values: &[TypeArg],
_def: &'o hugr::extension::OpDef,
_extension_registry: &ExtensionRegistry,
) -> Result<PolyFuncType, SignatureError> {
let [TypeArg::Opaque { arg }] = arg_values else {
// This should have already been checked.
panic!("Wrong number of arguments");
};
let op: JsonOp = serde_yaml::from_value(arg.value.clone()).unwrap(); // TODO Errors!
Ok(op.signature().into())
}

fn static_params(&self) -> &[TypeParam] {
&self.0
}
}

/// Angle type with given log denominator.
Expand All @@ -151,7 +158,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::<Tk2Op>(&mut e).expect("add fail");
Tk2Op::load_all_ops(&mut e).expect("add fail");

let sym_expr_opdef = e.add_type(
SYM_EXPR_NAME,
Expand All @@ -160,13 +167,12 @@ pub static ref TKET2_EXTENSION: Extension = {
TypeBound::Eq.into(),
)
.unwrap();
let sym_expr_param = TypeParam::Opaque(sym_expr_opdef.instantiate([]).unwrap());
let sym_expr_param = TypeParam::Opaque{ty:sym_expr_opdef.instantiate([]).unwrap()};

e.add_op_custom_sig_simple(
e.add_op(
SYM_OP_ID,
"Store a sympy expression that can be evaluated to a float.".to_string(),
vec![sym_expr_param],
|_: &[TypeArg]| Ok(FunctionType::new(type_row![], type_row![FLOAT64_TYPE])),
PolyFuncType::new(vec![sym_expr_param], FunctionType::new(type_row![], type_row![FLOAT64_TYPE])),
)
.unwrap();

Expand Down
Loading

0 comments on commit 9ef975e

Please sign in to comment.