Skip to content

Commit 5ae0ab9

Browse files
tatiana-sdoug-qmark-kochss2165aborgna-q
authored
feat!: Add ReplaceBoolPass (#854)
To replace `LazifyMeasurePass`, see quantinuum-dev/hugrverse#130 Closes #857 BREAKING CHANGE: (`tket2.bool` extension) `BoolOp::bool_to_sum` / `BoolOp::sum_to_bool` renamed to `BoolOp::read` / `BoolOp::make_opaque` BREAKING CHANGE: `Tk2Op::MeasureFree` now returns a `tket2.bool` BREAKING CHANGE: (`tket2-hseries.qsystem` extension) `QSystemOp:Measure` and `QSystemOp:MeasureReset` now return `tket2.bool`s --------- Co-authored-by: Douglas Wilson <[email protected]> Co-authored-by: Mark Koch <[email protected]> Co-authored-by: Seyon Sivarajah <[email protected]> Co-authored-by: Agustín Borgna <[email protected]> Co-authored-by: Luca Mondada <[email protected]>
1 parent e2e7e09 commit 5ae0ab9

File tree

11 files changed

+527
-543
lines changed

11 files changed

+527
-543
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tket2-exts/src/tket2_exts/data/tket2/bool.json

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,21 @@
5050
},
5151
"binary": false
5252
},
53-
"bool_to_sum": {
53+
"eq": {
5454
"extension": "tket2.bool",
55-
"name": "bool_to_sum",
56-
"description": "Convert a tket2.bool into a Hugr unit sum.",
55+
"name": "eq",
56+
"description": "Equality between two tket2.bools.",
5757
"signature": {
5858
"params": [],
5959
"body": {
6060
"input": [
61+
{
62+
"t": "Opaque",
63+
"extension": "tket2.bool",
64+
"id": "bool",
65+
"args": [],
66+
"bound": "C"
67+
},
6168
{
6269
"t": "Opaque",
6370
"extension": "tket2.bool",
@@ -68,36 +75,29 @@
6875
],
6976
"output": [
7077
{
71-
"t": "Sum",
72-
"s": "Unit",
73-
"size": 2
78+
"t": "Opaque",
79+
"extension": "tket2.bool",
80+
"id": "bool",
81+
"args": [],
82+
"bound": "C"
7483
}
7584
]
7685
}
7786
},
7887
"binary": false
7988
},
80-
"eq": {
89+
"make_opaque": {
8190
"extension": "tket2.bool",
82-
"name": "eq",
83-
"description": "Equality between two tket2.bools.",
91+
"name": "make_opaque",
92+
"description": "Convert a Hugr bool_t (a unit sum) into an tket2.bool.",
8493
"signature": {
8594
"params": [],
8695
"body": {
8796
"input": [
8897
{
89-
"t": "Opaque",
90-
"extension": "tket2.bool",
91-
"id": "bool",
92-
"args": [],
93-
"bound": "C"
94-
},
95-
{
96-
"t": "Opaque",
97-
"extension": "tket2.bool",
98-
"id": "bool",
99-
"args": [],
100-
"bound": "C"
98+
"t": "Sum",
99+
"s": "Unit",
100+
"size": 2
101101
}
102102
],
103103
"output": [
@@ -178,28 +178,28 @@
178178
},
179179
"binary": false
180180
},
181-
"sum_to_bool": {
181+
"read": {
182182
"extension": "tket2.bool",
183-
"name": "sum_to_bool",
184-
"description": "Convert a Hugr unit sum into an tket2.bool.",
183+
"name": "read",
184+
"description": "Convert a tket2.bool into a Hugr bool_t (a unit sum).",
185185
"signature": {
186186
"params": [],
187187
"body": {
188188
"input": [
189-
{
190-
"t": "Sum",
191-
"s": "Unit",
192-
"size": 2
193-
}
194-
],
195-
"output": [
196189
{
197190
"t": "Opaque",
198191
"extension": "tket2.bool",
199192
"id": "bool",
200193
"args": [],
201194
"bound": "C"
202195
}
196+
],
197+
"output": [
198+
{
199+
"t": "Sum",
200+
"s": "Unit",
201+
"size": 2
202+
}
203203
]
204204
}
205205
},

tket2-exts/src/tket2_exts/data/tket2/qsystem.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@
8888
],
8989
"output": [
9090
{
91-
"t": "Sum",
92-
"s": "Unit",
93-
"size": 2
91+
"t": "Opaque",
92+
"extension": "tket2.bool",
93+
"id": "bool",
94+
"args": [],
95+
"bound": "C"
9496
}
9597
]
9698
}
@@ -114,9 +116,11 @@
114116
"t": "Q"
115117
},
116118
{
117-
"t": "Sum",
118-
"s": "Unit",
119-
"size": 2
119+
"t": "Opaque",
120+
"extension": "tket2.bool",
121+
"id": "bool",
122+
"args": [],
123+
"bound": "C"
120124
}
121125
]
122126
}

tket2-exts/src/tket2_exts/data/tket2/quantum.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@
223223
],
224224
"output": [
225225
{
226-
"t": "Sum",
227-
"s": "Unit",
228-
"size": 2
226+
"t": "Opaque",
227+
"extension": "tket2.bool",
228+
"id": "bool",
229+
"args": [],
230+
"bound": "C"
229231
}
230232
]
231233
}

tket2-hseries/src/extension/qsystem.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::extension::futures;
3333
use derive_more::Display;
3434
use lazy_static::lazy_static;
3535
use strum::{EnumIter, EnumString, IntoStaticStr};
36+
use tket2::extension::bool::{bool_type, BoolOp};
3637

3738
use super::futures::future_type;
3839

@@ -110,12 +111,12 @@ impl MakeOpDef for QSystemOp {
110111
LazyMeasureReset => Signature::new(qb_t(), vec![qb_t(), future_type(bool_t())]),
111112
Reset => Signature::new(one_qb_row.clone(), one_qb_row),
112113
ZZPhase => Signature::new(vec![qb_t(), qb_t(), float64_type()], two_qb_row),
113-
Measure => Signature::new(one_qb_row, bool_t()),
114+
Measure => Signature::new(one_qb_row, bool_type()),
114115
Rz => Signature::new(vec![qb_t(), float64_type()], one_qb_row),
115116
PhasedX => Signature::new(vec![qb_t(), float64_type(), float64_type()], one_qb_row),
116117
TryQAlloc => Signature::new(type_row![], Type::from(option_type(one_qb_row))),
117118
QFree => Signature::new(one_qb_row, type_row![]),
118-
MeasureReset => Signature::new(one_qb_row.clone(), vec![qb_t(), bool_t()]),
119+
MeasureReset => Signature::new(one_qb_row.clone(), vec![qb_t(), bool_type()]),
119120
}
120121
.into()
121122
}
@@ -452,8 +453,9 @@ pub trait QSystemOpBuilder: Dataflow + UnwrapBuilder + ArrayOpBuilder {
452453
/// Build a projective measurement with a conditional flip.
453454
fn build_measure_flip(&mut self, qb: Wire) -> Result<[Wire; 2], BuildError> {
454455
let [qb, b] = self.add_measure_reset(qb)?;
456+
let sum_b = self.add_dataflow_op(BoolOp::read, [b])?.out_wire(0);
455457
let mut conditional = self.conditional_builder(
456-
([type_row![], type_row![]], b),
458+
([type_row![], type_row![]], sum_b),
457459
[(qb_t(), qb)],
458460
vec![qb_t()].into(),
459461
)?;
@@ -470,7 +472,7 @@ pub trait QSystemOpBuilder: Dataflow + UnwrapBuilder + ArrayOpBuilder {
470472
case1.finish_with_outputs([qb])?;
471473

472474
let [qb] = conditional.finish_sub_container()?.outputs_arr();
473-
Ok([qb, b])
475+
Ok([qb, sum_b])
474476
}
475477

476478
/// Build a qalloc operation that panics on failure.
@@ -591,7 +593,7 @@ mod test {
591593
let hugr = {
592594
let mut func_builder = FunctionBuilder::new(
593595
"all_ops",
594-
Signature::new(vec![qb_t(), float64_type()], vec![bool_t()]),
596+
Signature::new(vec![qb_t(), float64_type()], vec![bool_type()]),
595597
)
596598
.unwrap();
597599
let [q0, angle] = func_builder.input_wires_arr();

tket2-hseries/src/extension/qsystem/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ mod test {
378378
assert_eq!(lowered.len(), 6);
379379
// dfg, input, output, alloc + (10 for unwrap), phasedx, rz, toturns, fmul, phasedx, free +
380380
// 5x(float + load), measure_reset, conditional, case(input, output) * 2, flip
381-
// (phasedx + 2*(float + load))
381+
// (phasedx + 2*(float + load)), tket2.read
382382
// + 19 for the barrier array wrapping, popping and option unwrapping
383-
assert_eq!(h.descendants(h.module_root()).count(), 82);
383+
assert_eq!(h.descendants(h.module_root()).count(), 83);
384384
assert_eq!(check_lowered(&h), Ok(()));
385385
if let Err(e) = h.validate() {
386386
panic!("{}", e);

0 commit comments

Comments
 (0)