Classical operations composed of several binary operations incorrectly converted to PHIR
Closed this issue · 0 comments
cqc-alec commented
from pytket.circuit import Circuit
from pytket.phir.api import pytket_to_phir
circ = Circuit(4)
a = circ.add_c_register("a", 4)
b = circ.add_c_register("b", 1)
circ.add_classicalexpbox_bit(a[0] ^ a[1] ^ a[2] ^ a[3], [b[0]])
print(pytket_to_phir(circ))
Output:
{
"format": "PHIR/JSON",
"version": "0.1.0",
"metadata": {
"source": "pytket-phir v0.4.3"
},
"ops": [
{
"data": "cvar_define",
"data_type": "u32",
"variable": "b",
"size": 1
},
{
"data": "cvar_define",
"data_type": "u32",
"variable": "a",
"size": 4
},
{
"//": "ClassicalExpBox a[0], a[1], a[2], a[3], b[0];"
},
{
"cop": "=",
"returns": [
[
"b",
0
]
],
"args": [
{
"cop": "^",
"args": [
{
"cop": "^",
"args": [
{
"cop": "^",
"args": [
"a",
"a"
]
},
"a"
]
},
[
"a",
3
]
]
}
]
}
]
}
Note that {"cop": "^", "args": [{"cop": "^", "args": ["a", "a"]}, "a"]}
includes the whole register a
in its arguments three times, instead of the bits a[0]
, a[1]
and a[2]
.
This leads to incorrect simulation results.
Reproduced with pytket-phir 0.4.3, pytket 1.25.0.