diff --git a/pytket/phir/sharding/sharder.py b/pytket/phir/sharding/sharder.py index 09d8762..6e31004 100644 --- a/pytket/phir/sharding/sharder.py +++ b/pytket/phir/sharding/sharder.py @@ -273,7 +273,7 @@ def should_op_create_shard(op: Op) -> bool: """ return ( op.type in SHARD_TRIGGER_OP_TYPES - or (isinstance(op, Conditional) and op.op.type in SHARD_TRIGGER_OP_TYPES) + or isinstance(op, Conditional) or (op.is_gate() and op.n_qubits > 1) ) diff --git a/tests/data/phase.json b/tests/data/phase.json deleted file mode 100644 index 8ddaece..0000000 --- a/tests/data/phase.json +++ /dev/null @@ -1,2193 +0,0 @@ -{ - "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 7b4eba5..6153229 100644 --- a/tests/test_phirgen.py +++ b/tests/test_phirgen.py @@ -9,7 +9,6 @@ # mypy: disable-error-code="misc" import json -from pathlib import Path from pytket.circuit import Circuit from pytket.phir.api import pytket_to_phir @@ -118,16 +117,6 @@ def test_nested_bitwise_op() -> None: } -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)) - assert phir["ops"][-7]["true_branch"] == [{"mop": "Skip"}] - - def test_sleep_idle() -> None: """Ensure sleep from qasm gets converted to PHIR Idle Mop.""" circ = get_qasm_as_circuit(QasmFile.sleep) @@ -150,3 +139,37 @@ def test_multiple_sleep() -> None: phir = json.loads(pytket_to_phir(circ)) assert phir["ops"][2] == {"mop": "Idle", "args": [["q", 0]], "duration": [1.0, "s"]} assert phir["ops"][4] == {"mop": "Idle", "args": [["q", 1]], "duration": [2.0, "s"]} + + +def test_reordering_classical_conditional() -> None: + """From https://github.com/CQCL/pytket-phir/issues/150 .""" + circuit = Circuit(1) + + ctrl = circuit.add_c_register(name="ctrl", size=1) + meas = circuit.add_c_register(name="meas", size=1) + + circuit.add_c_setreg(1, ctrl) + circuit.X(0, condition=ctrl[0]) + + circuit.add_c_setreg(0, ctrl) + circuit.X(0, condition=ctrl[0]) + + circuit.Measure( + qubit=circuit.qubits[0], + bit=meas[0], + ) + + phir = json.loads(pytket_to_phir(circuit)) + + assert phir["ops"][4] == {"args": [1], "cop": "=", "returns": [["ctrl", 0]]} + assert phir["ops"][6] == { + "block": "if", + "condition": {"args": [["ctrl", 0], 1], "cop": "=="}, + "true_branch": [{"angles": None, "args": [["q", 0]], "qop": "X"}], + } + assert phir["ops"][8] == {"args": [0], "cop": "=", "returns": [["ctrl", 0]]} + assert phir["ops"][10] == { + "block": "if", + "condition": {"args": [["ctrl", 0], 1], "cop": "=="}, + "true_branch": [{"angles": None, "args": [["q", 0]], "qop": "X"}], + } diff --git a/tests/test_sharder.py b/tests/test_sharder.py index 4b1a3ff..b777d36 100644 --- a/tests/test_sharder.py +++ b/tests/test_sharder.py @@ -103,7 +103,7 @@ def test_simple_conditional(self) -> None: circuit = get_qasm_as_circuit(QasmFile.simple_cond) shards = Sharder(circuit).shard() - assert len(shards) == 4 + assert len(shards) == 5 # shard 0: [h q;] measure q->c; assert shards[0].primary_command.op.type == OpType.Measure @@ -132,18 +132,20 @@ def test_simple_conditional(self) -> None: assert shards[2].bits_read == {circuit.bits[0], circuit.bits[1]} assert shards[2].depends_upon == {shards[0].ID} - # shard 3: [if (c==1) h q;] measure q->c; - assert shards[3].primary_command.op.type == OpType.Measure + # shard 3: [if (c==1) h q;] + assert isinstance(shards[3].primary_command.op, Conditional) + assert shards[3].primary_command.op.op.type == OpType.H + assert not shards[3].sub_commands assert shards[3].qubits_used == {circuit.qubits[0]} - assert shards[3].bits_written == {circuit.bits[0]} assert shards[3].bits_read == {circuit.bits[0]} - assert shards[3].depends_upon == {shards[0].ID, shards[1].ID, shards[2].ID} - assert len(shards[3].sub_commands.items()) == 1 - s2_qubit, s2_sub_cmds = next(iter(shards[3].sub_commands.items())) - assert s2_qubit == circuit.qubits[0] - assert isinstance(s2_sub_cmds[0].op, Conditional) - assert s2_sub_cmds[0].op.op.type == OpType.H - assert s2_sub_cmds[0].qubits == [circuit.qubits[0]] + assert shards[3].depends_upon == {shards[0].ID, shards[1].ID} + + # shard 4: measure q->c; + assert shards[4].primary_command.op.type == OpType.Measure + assert not shards[4].sub_commands + assert shards[4].qubits_used == {circuit.qubits[0]} + assert shards[4].bits_written == {circuit.bits[0]} + assert shards[4].depends_upon == {shards[0].ID, shards[3].ID} def test_complex_barriers(self) -> None: # noqa: PLR0915 circuit = get_qasm_as_circuit(QasmFile.barrier_complex)