From 3949c6fb69cde4d9ad1b6091727a2aadb062bac6 Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Fri, 23 Feb 2024 16:04:34 -0600 Subject: [PATCH] fix(phirgen): emit Skip mop instead of error on global phase Fixes: #136 --- pytket/phir/phirgen.py | 8 +- requirements.txt | 4 +- tests/data/phase.json | 2193 ++++++++++++++++++++++++++++++++++++++++ tests/test_phirgen.py | 28 +- 4 files changed, 2219 insertions(+), 14 deletions(-) create mode 100644 tests/data/phase.json diff --git a/pytket/phir/phirgen.py b/pytket/phir/phirgen.py index 676cd3f..b7b65c7 100644 --- a/pytket/phir/phirgen.py +++ b/pytket/phir/phirgen.py @@ -175,6 +175,9 @@ def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None: try: gate = tket_gate_to_phir[op.type] except KeyError: + if op.type == tk.OpType.Phase: + # ignore global phase + return {"mop": "Skip"} logging.exception("Gate %s unsupported by PHIR", op.get_name()) raise angles = (op.params, "pi") if op.params else None @@ -186,8 +189,7 @@ def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None: "returns": [arg_to_bit(cmd.bits[0])], "args": [arg_to_bit(cmd.args[0])], } - case ( - "CX" + case ("CX" | "CY" | "CZ" | "RXX" @@ -201,7 +203,7 @@ def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None: | "SZZ" | "SZZdg" | "SWAP" - ): # two-qubit gates + ): # two-qubit gates # fmt: skip qop = { "qop": gate, "angles": angles, diff --git a/requirements.txt b/requirements.txt index cb7f1d5..77faac7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,10 +4,10 @@ networkx==2.8.8 phir==0.3.0 pre-commit==3.6.2 pydata_sphinx_theme==0.15.2 -pytest==8.0.1 +pytest==8.0.2 pytket==1.25.0 ruff==0.2.2 setuptools_scm==8.0.4 sphinx==7.2.6 -wasmtime==17.0.1 +wasmtime==18.0.0 wheel==0.42.0 diff --git a/tests/data/phase.json b/tests/data/phase.json new file mode 100644 index 0000000..8ddaece --- /dev/null +++ b/tests/data/phase.json @@ -0,0 +1,2193 @@ +{ + "bits": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "commands": [ + { + "args": [ + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "init_corrections", + "n": 0, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [], + "width_o_parameter": [], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "x_corr_0", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "x_corr_1", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "x_corr_2", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "z_corr_0", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "z_corr_1", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "z_corr_2", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "q", + [2] + ] + ], + "op": + { + "type": "Reset" + } + }, + { + "args": [ + [ + "q", + [2] + ] + ], + "op": + { + "params": ["1/2","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [2] + ] + ], + "op": + { + "params": ["1"], + "type": "Rz" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "x_corr_3", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "classical": + { + "values": [false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "q", + [3] + ] + ], + "op": + { + "type": "Reset" + } + }, + { + "args": [ + [ + "q", + [3] + ] + ], + "op": + { + "params": ["1/2","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [3] + ] + ], + "op": + { + "params": ["1"], + "type": "Rz" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "q", + [2] + ] + ], + "op": + { + "params": ["1","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [3] + ] + ], + "op": + { + "params": ["1","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [2] + ] + ], + "op": + { + "params": ["0.5"], + "type": "ZZPhase" + } + }, + { + "args": [ + [ + "q", + [1] + ], + [ + "q", + [3] + ] + ], + "op": + { + "params": ["0.5"], + "type": "ZZPhase" + } + }, + { + "args": [ + [ + "q", + [0] + ] + ], + "op": + { + "params": ["1/2"], + "type": "Rz" + } + }, + { + "args": [ + [ + "q", + [1] + ] + ], + "op": + { + "params": ["1/2"], + "type": "Rz" + } + }, + { + "args": [ + [ + "q", + [2] + ] + ], + "op": + { + "params": ["1","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [3] + ] + ], + "op": + { + "params": ["1","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [2] + ] + ], + "op": + { + "params": ["1/2"], + "type": "Rz" + } + }, + { + "args": [ + [ + "q", + [3] + ] + ], + "op": + { + "params": ["1/2"], + "type": "Rz" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ] + ], + "op": + { + "classical": + { + "values": [false,false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "x_corr_0", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_x_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "z_corr_0", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_z_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "x_corr_0", + [0] + ], + [ + "q", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1","0"], + "type": "PhasedX" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "x_corr_0", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_0", + [0] + ], + [ + "q", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1"], + "type": "Rz" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_0", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "q", + [0] + ] + ], + "op": + { + "params": ["1/2","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "meas_0", + [0] + ] + ], + "op": + { + "type": "Measure" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ] + ], + "op": + { + "classical": + { + "values": [false,true] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "meas_0", + [0] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "update_x_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [1,2], + "width_o_parameter": [], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ] + ], + "op": + { + "classical": + { + "values": [true,false] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "x_corr_1", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_x_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "z_corr_1", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_z_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "x_corr_1", + [0] + ], + [ + "q", + [1] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1","0"], + "type": "PhasedX" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "x_corr_1", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_1", + [0] + ], + [ + "q", + [1] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1"], + "type": "Rz" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_1", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "q", + [1] + ] + ], + "op": + { + "params": ["1/2","-1/2"], + "type": "PhasedX" + } + }, + { + "args": [ + [ + "q", + [1] + ], + [ + "meas_1", + [0] + ] + ], + "op": + { + "type": "Measure" + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ] + ], + "op": + { + "classical": + { + "values": [true,true] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "meas_1", + [0] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "update_x_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [1,2], + "width_o_parameter": [], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ], + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "meas_0", + [0] + ], + [ + "meas_1", + [0] + ], + [ + "meas_2", + [0] + ], + [ + "meas_3", + [0] + ], + [ + "x_corr_0", + [0] + ], + [ + "x_corr_1", + [0] + ], + [ + "x_corr_2", + [0] + ], + [ + "x_corr_3", + [0] + ], + [ + "z_corr_0", + [0] + ], + [ + "z_corr_1", + [0] + ], + [ + "z_corr_2", + [0] + ], + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "data": "", + "signature": ["Q","Q","Q","Q","C","C","C","C","C","C","C","C","C","C","C","C","C","C"], + "type": "Barrier" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ] + ], + "op": + { + "classical": + { + "values": [false,true] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "x_corr_2", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_x_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "z_corr_2", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_z_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "x_corr_2", + [0] + ], + [ + "q", + [2] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1","0"], + "type": "PhasedX" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "x_corr_2", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ] + ], + "op": + { + "classical": + { + "values": [true,true] + }, + "type": "SetBits" + } + }, + { + "args": [ + [ + "z_corr_2", + [0] + ], + [ + "q", + [2] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1"], + "type": "Rz" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_2", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "x_corr_3", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_x_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "index", + [0] + ], + [ + "index", + [1] + ], + [ + "z_corr_3", + [0] + ], + [ + "_w", + [0] + ] + ], + "op": + { + "type": "WASM", + "wasm": + { + "func_name": "get_z_correction", + "n": 3, + "wasm_file_uid": "89f01a2184dfa21ffb64388bd441a12e545c0e177d8312fd5780b51d501f1b4c", + "width_i_parameter": [2], + "width_o_parameter": [1], + "ww_n": 1 + } + } + }, + { + "args": [ + [ + "x_corr_3", + [0] + ], + [ + "q", + [3] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1","0"], + "type": "PhasedX" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "x_corr_3", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_3", + [0] + ], + [ + "q", + [3] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["1"], + "type": "Rz" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + }, + { + "args": [ + [ + "z_corr_3", + [0] + ] + ], + "op": + { + "conditional": + { + "op": + { + "params": ["0.5"], + "type": "Phase" + }, + "value": 1, + "width": 1 + }, + "type": "Conditional" + } + } + ], + "created_qubits": [], + "discarded_qubits": [], + "implicit_permutation": [ + [ + [ + "q", + [0] + ], + [ + "q", + [0] + ] + ], + [ + [ + "q", + [1] + ], + [ + "q", + [1] + ] + ], + [ + [ + "q", + [2] + ], + [ + "q", + [2] + ] + ], + [ + [ + "q", + [3] + ], + [ + "q", + [3] + ] + ] + ], + "number_of_ws": 1, + "phase": "0.5", + "qubits": [ + [ + "q", + [0] + ], + [ + "q", + [1] + ], + [ + "q", + [2] + ], + [ + "q", + [3] + ] + ] +} diff --git a/tests/test_phirgen.py b/tests/test_phirgen.py index fa5e7d1..f88d3fd 100644 --- a/tests/test_phirgen.py +++ b/tests/test_phirgen.py @@ -9,6 +9,7 @@ # mypy: disable-error-code="misc" import json +from pathlib import Path from pytket.circuit import Circuit from pytket.phir.api import pytket_to_phir @@ -79,6 +80,17 @@ def test_bitwise_ops() -> None: } +def test_conditional_barrier() -> None: + """From https://github.com/CQCL/pytket-phir/issues/119 .""" + circ = get_qasm_as_circuit(QasmFile.cond_barrier) + phir = json.loads(pytket_to_phir(circ)) + assert phir["ops"][5] == { + "block": "if", + "condition": {"cop": "==", "args": ["m", 0]}, + "true_branch": [{"meta": "barrier", "args": [["q", 0], ["q", 1]]}], + } + + def test_nested_bitwise_op() -> None: """From https://github.com/CQCL/pytket-phir/issues/133 .""" circ = Circuit(4) @@ -105,12 +117,10 @@ def test_nested_bitwise_op() -> None: } -def test_conditional_barrier() -> None: - """From https://github.com/CQCL/pytket-phir/issues/119 .""" - circ = get_qasm_as_circuit(QasmFile.cond_barrier) - phir = json.loads(pytket_to_phir(circ)) - assert phir["ops"][5] == { - "block": "if", - "condition": {"cop": "==", "args": ["m", 0]}, - "true_branch": [{"meta": "barrier", "args": [["q", 0], ["q", 1]]}], - } +def test_global_phase() -> None: + """From https://github.com/CQCL/pytket-phir/issues/136 .""" + this_dir = Path(Path(__file__).resolve()).parent + with Path(f"{this_dir}/data/phase.json").open() as fp: + circ = Circuit.from_dict(json.load(fp)) + + _phir = json.loads(pytket_to_phir(circ))