cirq-core 1.6.0.dev20250609223500__py3-none-any.whl → 1.6.0.dev20250612013443__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cirq-core might be problematic. Click here for more details.
- cirq/_version.py +1 -1
- cirq/_version_test.py +1 -1
- cirq/contrib/qasm_import/_parser.py +447 -6
- cirq/contrib/qasm_import/_parser_test.py +768 -35
- cirq/transformers/dynamical_decoupling.py +16 -6
- cirq/transformers/dynamical_decoupling_test.py +211 -193
- {cirq_core-1.6.0.dev20250609223500.dist-info → cirq_core-1.6.0.dev20250612013443.dist-info}/METADATA +1 -1
- {cirq_core-1.6.0.dev20250609223500.dist-info → cirq_core-1.6.0.dev20250612013443.dist-info}/RECORD +11 -11
- {cirq_core-1.6.0.dev20250609223500.dist-info → cirq_core-1.6.0.dev20250612013443.dist-info}/WHEEL +0 -0
- {cirq_core-1.6.0.dev20250609223500.dist-info → cirq_core-1.6.0.dev20250612013443.dist-info}/licenses/LICENSE +0 -0
- {cirq_core-1.6.0.dev20250609223500.dist-info → cirq_core-1.6.0.dev20250612013443.dist-info}/top_level.txt +0 -0
|
@@ -216,6 +216,8 @@ def test_CX_gate() -> None:
|
|
|
216
216
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
217
217
|
assert parsed_qasm.qregs == {'q1': 2, 'q2': 2}
|
|
218
218
|
|
|
219
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
220
|
+
|
|
219
221
|
|
|
220
222
|
def test_classical_control() -> None:
|
|
221
223
|
qasm = """OPENQASM 2.0;
|
|
@@ -377,6 +379,8 @@ def test_U_gate() -> None:
|
|
|
377
379
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
378
380
|
assert parsed_qasm.qregs == {'q': 2}
|
|
379
381
|
|
|
382
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
383
|
+
|
|
380
384
|
|
|
381
385
|
def test_U_angles() -> None:
|
|
382
386
|
qasm = """
|
|
@@ -464,6 +468,8 @@ def test_expressions(expr: str) -> None:
|
|
|
464
468
|
)
|
|
465
469
|
assert parsed_qasm.qregs == {'q': 1}
|
|
466
470
|
|
|
471
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
472
|
+
|
|
467
473
|
|
|
468
474
|
def test_unknown_function() -> None:
|
|
469
475
|
qasm = """OPENQASM 2.0;
|
|
@@ -480,6 +486,7 @@ rotation_gates = [('rx', cirq.rx), ('ry', cirq.ry), ('rz', cirq.rz)]
|
|
|
480
486
|
|
|
481
487
|
|
|
482
488
|
single_qubit_gates = [
|
|
489
|
+
('id', cirq.I),
|
|
483
490
|
('x', cirq.X),
|
|
484
491
|
('y', cirq.Y),
|
|
485
492
|
('z', cirq.Z),
|
|
@@ -489,6 +496,7 @@ single_qubit_gates = [
|
|
|
489
496
|
('sdg', cirq.S**-1),
|
|
490
497
|
('tdg', cirq.T**-1),
|
|
491
498
|
('sx', cirq.XPowGate(exponent=0.5)),
|
|
499
|
+
('sxdg', cirq.XPowGate(exponent=-0.5)),
|
|
492
500
|
]
|
|
493
501
|
|
|
494
502
|
|
|
@@ -518,6 +526,8 @@ def test_rotation_gates(qasm_gate: str, cirq_gate: Callable[[float], cirq.Gate])
|
|
|
518
526
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
519
527
|
assert parsed_qasm.qregs == {'q': 2}
|
|
520
528
|
|
|
529
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
530
|
+
|
|
521
531
|
|
|
522
532
|
@pytest.mark.parametrize('qasm_gate', [g[0] for g in rotation_gates])
|
|
523
533
|
def test_rotation_gates_wrong_number_of_args(qasm_gate: str) -> None:
|
|
@@ -610,6 +620,8 @@ def test_measure_individual_bits() -> None:
|
|
|
610
620
|
assert parsed_qasm.qregs == {'q1': 2}
|
|
611
621
|
assert parsed_qasm.cregs == {'c1': 2}
|
|
612
622
|
|
|
623
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
624
|
+
|
|
613
625
|
|
|
614
626
|
def test_measure_registers() -> None:
|
|
615
627
|
qasm = """OPENQASM 2.0;
|
|
@@ -639,6 +651,8 @@ def test_measure_registers() -> None:
|
|
|
639
651
|
assert parsed_qasm.qregs == {'q1': 3}
|
|
640
652
|
assert parsed_qasm.cregs == {'c1': 3}
|
|
641
653
|
|
|
654
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
655
|
+
|
|
642
656
|
|
|
643
657
|
def test_measure_mismatched_register_size() -> None:
|
|
644
658
|
qasm = """OPENQASM 2.0;
|
|
@@ -739,6 +753,31 @@ def test_reset() -> None:
|
|
|
739
753
|
assert parsed_qasm.cregs == {'c': 1}
|
|
740
754
|
|
|
741
755
|
|
|
756
|
+
def test_u0_gate() -> None:
|
|
757
|
+
qasm = """
|
|
758
|
+
OPENQASM 2.0;
|
|
759
|
+
include "qelib1.inc";
|
|
760
|
+
qreg q[1];
|
|
761
|
+
u0(0) q[0];
|
|
762
|
+
"""
|
|
763
|
+
parser = QasmParser()
|
|
764
|
+
|
|
765
|
+
q0 = cirq.NamedQubit('q_0')
|
|
766
|
+
|
|
767
|
+
expected_circuit = Circuit()
|
|
768
|
+
expected_circuit.append(cirq.I(q0))
|
|
769
|
+
|
|
770
|
+
parsed_qasm = parser.parse(qasm)
|
|
771
|
+
|
|
772
|
+
assert parsed_qasm.supportedFormat
|
|
773
|
+
assert parsed_qasm.qelib1Include
|
|
774
|
+
|
|
775
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
776
|
+
assert parsed_qasm.qregs == {'q': 1}
|
|
777
|
+
|
|
778
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
779
|
+
|
|
780
|
+
|
|
742
781
|
def test_u1_gate() -> None:
|
|
743
782
|
qasm = """
|
|
744
783
|
OPENQASM 2.0;
|
|
@@ -761,6 +800,33 @@ def test_u1_gate() -> None:
|
|
|
761
800
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
762
801
|
assert parsed_qasm.qregs == {'q': 1}
|
|
763
802
|
|
|
803
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
def test_p_gate() -> None:
|
|
807
|
+
qasm = """
|
|
808
|
+
OPENQASM 2.0;
|
|
809
|
+
include "qelib1.inc";
|
|
810
|
+
qreg q[1];
|
|
811
|
+
p(pi / 3.0) q[0];
|
|
812
|
+
"""
|
|
813
|
+
parser = QasmParser()
|
|
814
|
+
|
|
815
|
+
q0 = cirq.NamedQubit('q_0')
|
|
816
|
+
|
|
817
|
+
expected_circuit = Circuit()
|
|
818
|
+
expected_circuit.append(cirq.ZPowGate(exponent=1.0 / 3.0)(q0))
|
|
819
|
+
|
|
820
|
+
parsed_qasm = parser.parse(qasm)
|
|
821
|
+
|
|
822
|
+
assert parsed_qasm.supportedFormat
|
|
823
|
+
assert parsed_qasm.qelib1Include
|
|
824
|
+
|
|
825
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
826
|
+
assert parsed_qasm.qregs == {'q': 1}
|
|
827
|
+
|
|
828
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
829
|
+
|
|
764
830
|
|
|
765
831
|
def test_u2_gate() -> None:
|
|
766
832
|
qasm = """
|
|
@@ -784,6 +850,8 @@ def test_u2_gate() -> None:
|
|
|
784
850
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
785
851
|
assert parsed_qasm.qregs == {'q': 1}
|
|
786
852
|
|
|
853
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
854
|
+
|
|
787
855
|
|
|
788
856
|
def test_id_gate() -> None:
|
|
789
857
|
qasm = """
|
|
@@ -809,6 +877,8 @@ def test_id_gate() -> None:
|
|
|
809
877
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
810
878
|
assert parsed_qasm.qregs == {'q': 2}
|
|
811
879
|
|
|
880
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
881
|
+
|
|
812
882
|
|
|
813
883
|
def test_u3_gate() -> None:
|
|
814
884
|
qasm = """
|
|
@@ -843,6 +913,44 @@ def test_u3_gate() -> None:
|
|
|
843
913
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
844
914
|
assert parsed_qasm.qregs == {'q': 2}
|
|
845
915
|
|
|
916
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
def test_u_gate() -> None:
|
|
920
|
+
qasm = """
|
|
921
|
+
OPENQASM 2.0;
|
|
922
|
+
include "qelib1.inc";
|
|
923
|
+
qreg q[2];
|
|
924
|
+
u(pi, 2.3, 3) q[0];
|
|
925
|
+
u(+3.14, -pi, (8)) q;
|
|
926
|
+
"""
|
|
927
|
+
parser = QasmParser()
|
|
928
|
+
|
|
929
|
+
q0 = cirq.NamedQubit('q_0')
|
|
930
|
+
q1 = cirq.NamedQubit('q_1')
|
|
931
|
+
|
|
932
|
+
expected_circuit = Circuit()
|
|
933
|
+
expected_circuit.append(
|
|
934
|
+
cirq.Moment(
|
|
935
|
+
[
|
|
936
|
+
QasmUGate(1.0, 2.3 / np.pi, 3 / np.pi)(q0),
|
|
937
|
+
QasmUGate(3.14 / np.pi, -1.0, 8 / np.pi)(q1),
|
|
938
|
+
]
|
|
939
|
+
)
|
|
940
|
+
)
|
|
941
|
+
|
|
942
|
+
expected_circuit.append(cirq.Moment([QasmUGate(3.14 / np.pi, -1.0, 8 / np.pi)(q0)]))
|
|
943
|
+
|
|
944
|
+
parsed_qasm = parser.parse(qasm)
|
|
945
|
+
|
|
946
|
+
assert parsed_qasm.supportedFormat
|
|
947
|
+
assert parsed_qasm.qelib1Include
|
|
948
|
+
|
|
949
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
950
|
+
assert parsed_qasm.qregs == {'q': 2}
|
|
951
|
+
|
|
952
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
953
|
+
|
|
846
954
|
|
|
847
955
|
def test_r_gate() -> None:
|
|
848
956
|
qasm = """
|
|
@@ -857,7 +965,6 @@ def test_r_gate() -> None:
|
|
|
857
965
|
|
|
858
966
|
expected_circuit = Circuit()
|
|
859
967
|
expected_circuit.append(QasmUGate(1.0, 0.0, 0.0)(q0))
|
|
860
|
-
|
|
861
968
|
parsed_qasm = parser.parse(qasm)
|
|
862
969
|
|
|
863
970
|
assert parsed_qasm.supportedFormat
|
|
@@ -869,7 +976,9 @@ def test_r_gate() -> None:
|
|
|
869
976
|
|
|
870
977
|
@pytest.mark.parametrize(
|
|
871
978
|
'qasm_gate',
|
|
872
|
-
['
|
|
979
|
+
['p', 'u0', 'u1', 'u2', 'r', 'u3']
|
|
980
|
+
+ [g[0] for g in rotation_gates]
|
|
981
|
+
+ [g[0] for g in single_qubit_gates],
|
|
873
982
|
)
|
|
874
983
|
def test_standard_single_qubit_gates_wrong_number_of_args(qasm_gate) -> None:
|
|
875
984
|
qasm = f"""
|
|
@@ -887,7 +996,18 @@ def test_standard_single_qubit_gates_wrong_number_of_args(qasm_gate) -> None:
|
|
|
887
996
|
|
|
888
997
|
@pytest.mark.parametrize(
|
|
889
998
|
['qasm_gate', 'num_params'],
|
|
890
|
-
[
|
|
999
|
+
[
|
|
1000
|
+
['u0', 1],
|
|
1001
|
+
['rx', 1],
|
|
1002
|
+
['ry', 1],
|
|
1003
|
+
['rz', 1],
|
|
1004
|
+
['p', 1],
|
|
1005
|
+
['u1', 1],
|
|
1006
|
+
['u2', 2],
|
|
1007
|
+
['r', 2],
|
|
1008
|
+
['u3', 3],
|
|
1009
|
+
['u', 3],
|
|
1010
|
+
]
|
|
891
1011
|
+ [[g[0], 0] for g in single_qubit_gates],
|
|
892
1012
|
)
|
|
893
1013
|
def test_standard_gates_wrong_params_error(qasm_gate: str, num_params: int) -> None:
|
|
@@ -924,14 +1044,20 @@ two_qubit_gates = [
|
|
|
924
1044
|
('cy', cirq.ControlledGate(cirq.Y)),
|
|
925
1045
|
('swap', cirq.SWAP),
|
|
926
1046
|
('ch', cirq.ControlledGate(cirq.H)),
|
|
1047
|
+
('csx', cirq.ControlledGate(cirq.XPowGate(exponent=0.5))),
|
|
927
1048
|
]
|
|
928
1049
|
|
|
929
1050
|
|
|
930
1051
|
# Mapping of two-qubit gates and `num_params`
|
|
931
1052
|
two_qubit_param_gates = {
|
|
932
|
-
|
|
933
|
-
('
|
|
1053
|
+
# TODO: fix and enable commented gates below
|
|
1054
|
+
# ('cu1', cirq.ControlledGate(QasmUGate(0, 0, 0.1 / np.pi))): 1,
|
|
1055
|
+
# ('cu3', cirq.ControlledGate(QasmUGate(0.1 / np.pi, 0.2 / np.pi, 0.3 / np.pi))): 3,
|
|
1056
|
+
# ('cu', cirq.ControlledGate(QasmUGate(0.1 / np.pi, 0.2 / np.pi, 0.3 / np.pi))): 3,
|
|
1057
|
+
('crx', cirq.ControlledGate(cirq.rx(0.1))): 1,
|
|
1058
|
+
('cry', cirq.ControlledGate(cirq.ry(0.1))): 1,
|
|
934
1059
|
('crz', cirq.ControlledGate(cirq.rz(0.1))): 1,
|
|
1060
|
+
('cp', cirq.ControlledGate(cirq.ZPowGate(exponent=0.1 / np.pi))): 1,
|
|
935
1061
|
}
|
|
936
1062
|
|
|
937
1063
|
|
|
@@ -971,6 +1097,8 @@ def test_two_qubit_gates(qasm_gate: str, cirq_gate: cirq.testing.TwoQubitGate) -
|
|
|
971
1097
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
972
1098
|
assert parsed_qasm.qregs == {'q1': 2, 'q2': 2}
|
|
973
1099
|
|
|
1100
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1101
|
+
|
|
974
1102
|
|
|
975
1103
|
@pytest.mark.parametrize(
|
|
976
1104
|
'qasm_gate,cirq_gate,num_params',
|
|
@@ -982,7 +1110,8 @@ def test_two_qubit_gates(qasm_gate: str, cirq_gate: cirq.testing.TwoQubitGate) -
|
|
|
982
1110
|
def test_two_qubit_param_gates(
|
|
983
1111
|
qasm_gate: str, cirq_gate: cirq.testing.TwoQubitGate, num_params: int
|
|
984
1112
|
) -> None:
|
|
985
|
-
params = '(0.1
|
|
1113
|
+
params = f"({', '.join(f'{0.1 * (x + 1):g}' for x in range(num_params))})"
|
|
1114
|
+
|
|
986
1115
|
qasm = f"""
|
|
987
1116
|
OPENQASM 2.0;
|
|
988
1117
|
include "qelib1.inc";
|
|
@@ -1013,31 +1142,30 @@ def test_two_qubit_param_gates(
|
|
|
1013
1142
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1014
1143
|
assert parsed_qasm.qregs == {'q1': 2, 'q2': 2}
|
|
1015
1144
|
|
|
1145
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1146
|
+
|
|
1016
1147
|
|
|
1017
1148
|
@pytest.mark.parametrize(
|
|
1018
1149
|
'qasm_gate', [g[0] for g in two_qubit_gates] + [g[0] for g in two_qubit_param_gates.keys()]
|
|
1019
1150
|
)
|
|
1020
1151
|
def test_two_qubit_gates_not_enough_qubits(qasm_gate: str) -> None:
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
"""
|
|
1035
|
-
else:
|
|
1036
|
-
qasm = f"""
|
|
1152
|
+
gate_mapping = {
|
|
1153
|
+
'crx': '(0.1)',
|
|
1154
|
+
'cry': '(0.1)',
|
|
1155
|
+
'crz': '(0.1)',
|
|
1156
|
+
'cp': '(0.1)',
|
|
1157
|
+
'cu1': '(0.1)',
|
|
1158
|
+
'cu3': '(0.1, 0.2, 0.3)',
|
|
1159
|
+
'cu': '(0.1, 0.2, 0.3)',
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
qasm_param = gate_mapping.get(qasm_gate, '')
|
|
1163
|
+
|
|
1164
|
+
qasm = f"""
|
|
1037
1165
|
OPENQASM 2.0;
|
|
1038
1166
|
include "qelib1.inc";
|
|
1039
1167
|
qreg q[2];
|
|
1040
|
-
{qasm_gate} q[0];
|
|
1168
|
+
{qasm_gate}{qasm_param} q[0];
|
|
1041
1169
|
"""
|
|
1042
1170
|
|
|
1043
1171
|
parser = QasmParser()
|
|
@@ -1065,10 +1193,9 @@ def test_two_qubit_gates_not_enough_args(qasm_gate: str) -> None:
|
|
|
1065
1193
|
'qasm_gate', [g[0] for g in two_qubit_gates] + [g[0] for g in two_qubit_param_gates.keys()]
|
|
1066
1194
|
)
|
|
1067
1195
|
def test_two_qubit_gates_with_too_much_parameters(qasm_gate: str) -> None:
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
num_params_needed = 0
|
|
1196
|
+
params_mapping = {'crx': 1, 'cry': 1, 'crz': 1, 'cp': 1, 'cu1': 1, 'cu3': 3, 'cu': 3}
|
|
1197
|
+
|
|
1198
|
+
num_params_needed = params_mapping.get(qasm_gate, 0)
|
|
1072
1199
|
|
|
1073
1200
|
qasm = f"""
|
|
1074
1201
|
OPENQASM 2.0;
|
|
@@ -1086,11 +1213,103 @@ def test_two_qubit_gates_with_too_much_parameters(qasm_gate: str) -> None:
|
|
|
1086
1213
|
parser.parse(qasm)
|
|
1087
1214
|
|
|
1088
1215
|
|
|
1089
|
-
three_qubit_gates = [
|
|
1216
|
+
three_qubit_gates = [
|
|
1217
|
+
('ccx', cirq.TOFFOLI),
|
|
1218
|
+
('cswap', cirq.CSWAP),
|
|
1219
|
+
(
|
|
1220
|
+
'rccx',
|
|
1221
|
+
cirq.MatrixGate(
|
|
1222
|
+
np.array(
|
|
1223
|
+
[
|
|
1224
|
+
[
|
|
1225
|
+
1.0 + 0.0j,
|
|
1226
|
+
0.0 + 0.0j,
|
|
1227
|
+
0.0 + 0.0j,
|
|
1228
|
+
0.0 + 0.0j,
|
|
1229
|
+
0.0 + 0.0j,
|
|
1230
|
+
0.0 + 0.0j,
|
|
1231
|
+
0.0 + 0.0j,
|
|
1232
|
+
0.0 + 0.0j,
|
|
1233
|
+
],
|
|
1234
|
+
[
|
|
1235
|
+
0.0 + 0.0j,
|
|
1236
|
+
1.0 + 0.0j,
|
|
1237
|
+
0.0 + 0.0j,
|
|
1238
|
+
0.0 + 0.0j,
|
|
1239
|
+
0.0 + 0.0j,
|
|
1240
|
+
0.0 + 0.0j,
|
|
1241
|
+
0.0 + 0.0j,
|
|
1242
|
+
0.0 + 0.0j,
|
|
1243
|
+
],
|
|
1244
|
+
[
|
|
1245
|
+
0.0 + 0.0j,
|
|
1246
|
+
0.0 + 0.0j,
|
|
1247
|
+
1.0 + 0.0j,
|
|
1248
|
+
0.0 + 0.0j,
|
|
1249
|
+
0.0 + 0.0j,
|
|
1250
|
+
0.0 + 0.0j,
|
|
1251
|
+
0.0 + 0.0j,
|
|
1252
|
+
0.0 + 0.0j,
|
|
1253
|
+
],
|
|
1254
|
+
[
|
|
1255
|
+
0.0 + 0.0j,
|
|
1256
|
+
0.0 + 0.0j,
|
|
1257
|
+
0.0 + 0.0j,
|
|
1258
|
+
1.0 + 0.0j,
|
|
1259
|
+
0.0 + 0.0j,
|
|
1260
|
+
0.0 + 0.0j,
|
|
1261
|
+
0.0 + 0.0j,
|
|
1262
|
+
0.0 + 0.0j,
|
|
1263
|
+
],
|
|
1264
|
+
[
|
|
1265
|
+
0.0 + 0.0j,
|
|
1266
|
+
0.0 + 0.0j,
|
|
1267
|
+
0.0 + 0.0j,
|
|
1268
|
+
0.0 + 0.0j,
|
|
1269
|
+
1.0 + 0.0j,
|
|
1270
|
+
0.0 + 0.0j,
|
|
1271
|
+
0.0 + 0.0j,
|
|
1272
|
+
0.0 + 0.0j,
|
|
1273
|
+
],
|
|
1274
|
+
[
|
|
1275
|
+
0.0 + 0.0j,
|
|
1276
|
+
0.0 + 0.0j,
|
|
1277
|
+
0.0 + 0.0j,
|
|
1278
|
+
0.0 + 0.0j,
|
|
1279
|
+
0.0 + 0.0j,
|
|
1280
|
+
-1.0 + 0.0j,
|
|
1281
|
+
0.0 + 0.0j,
|
|
1282
|
+
0.0 + 0.0j,
|
|
1283
|
+
],
|
|
1284
|
+
[
|
|
1285
|
+
0.0 + 0.0j,
|
|
1286
|
+
0.0 + 0.0j,
|
|
1287
|
+
0.0 + 0.0j,
|
|
1288
|
+
0.0 + 0.0j,
|
|
1289
|
+
0.0 + 0.0j,
|
|
1290
|
+
0.0 + 0.0j,
|
|
1291
|
+
0.0 + 0.0j,
|
|
1292
|
+
0.0 - 1.0j,
|
|
1293
|
+
],
|
|
1294
|
+
[
|
|
1295
|
+
0.0 + 0.0j,
|
|
1296
|
+
0.0 + 0.0j,
|
|
1297
|
+
0.0 + 0.0j,
|
|
1298
|
+
0.0 + 0.0j,
|
|
1299
|
+
0.0 + 0.0j,
|
|
1300
|
+
0.0 + 0.0j,
|
|
1301
|
+
0.0 + 1.0j,
|
|
1302
|
+
0.0 + 0.0j,
|
|
1303
|
+
],
|
|
1304
|
+
]
|
|
1305
|
+
)
|
|
1306
|
+
),
|
|
1307
|
+
),
|
|
1308
|
+
]
|
|
1090
1309
|
|
|
1091
1310
|
|
|
1092
1311
|
@pytest.mark.parametrize('qasm_gate,cirq_gate', three_qubit_gates)
|
|
1093
|
-
def test_three_qubit_gates(qasm_gate: str, cirq_gate: cirq.
|
|
1312
|
+
def test_three_qubit_gates(qasm_gate: str, cirq_gate: cirq.Gate) -> None:
|
|
1094
1313
|
qasm = f"""
|
|
1095
1314
|
OPENQASM 2.0;
|
|
1096
1315
|
include "qelib1.inc";
|
|
@@ -1128,6 +1347,8 @@ def test_three_qubit_gates(qasm_gate: str, cirq_gate: cirq.testing.TwoQubitGate)
|
|
|
1128
1347
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1129
1348
|
assert parsed_qasm.qregs == {'q1': 2, 'q2': 2, 'q3': 2}
|
|
1130
1349
|
|
|
1350
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1351
|
+
|
|
1131
1352
|
|
|
1132
1353
|
@pytest.mark.parametrize('qasm_gate', [g[0] for g in three_qubit_gates])
|
|
1133
1354
|
def test_three_qubit_gates_not_enough_args(qasm_gate: str) -> None:
|
|
@@ -1157,6 +1378,461 @@ def test_three_qubit_gates_with_too_much_parameters(qasm_gate: str) -> None:
|
|
|
1157
1378
|
parser.parse(qasm)
|
|
1158
1379
|
|
|
1159
1380
|
|
|
1381
|
+
four_qubit_gates = [
|
|
1382
|
+
('c3x', cirq.ControlledGate(cirq.X, num_controls=3)),
|
|
1383
|
+
('c3sqrtx', cirq.ControlledGate(cirq.XPowGate(exponent=0.5), num_controls=3)),
|
|
1384
|
+
(
|
|
1385
|
+
'rc3x',
|
|
1386
|
+
cirq.MatrixGate(
|
|
1387
|
+
np.array(
|
|
1388
|
+
[
|
|
1389
|
+
[
|
|
1390
|
+
1.0 + 0.0j,
|
|
1391
|
+
0.0 + 0.0j,
|
|
1392
|
+
0.0 + 0.0j,
|
|
1393
|
+
0.0 + 0.0j,
|
|
1394
|
+
0.0 + 0.0j,
|
|
1395
|
+
0.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 0.0j,
|
|
1676
|
+
],
|
|
1677
|
+
]
|
|
1678
|
+
)
|
|
1679
|
+
),
|
|
1680
|
+
),
|
|
1681
|
+
]
|
|
1682
|
+
|
|
1683
|
+
|
|
1684
|
+
@pytest.mark.parametrize('qasm_gate,cirq_gate', four_qubit_gates)
|
|
1685
|
+
def test_four_qubit_gates(qasm_gate: str, cirq_gate: cirq.Gate) -> None:
|
|
1686
|
+
qasm = f"""
|
|
1687
|
+
OPENQASM 2.0;
|
|
1688
|
+
include "qelib1.inc";
|
|
1689
|
+
qreg q1[2];
|
|
1690
|
+
qreg q2[2];
|
|
1691
|
+
qreg q3[2];
|
|
1692
|
+
qreg q4[2];
|
|
1693
|
+
{qasm_gate} q1[0], q1[1], q2[0], q3[0];
|
|
1694
|
+
{qasm_gate} q1, q2[0], q3[0], q4[0];
|
|
1695
|
+
{qasm_gate} q1, q2, q3, q4;
|
|
1696
|
+
"""
|
|
1697
|
+
parser = QasmParser()
|
|
1698
|
+
|
|
1699
|
+
q1_0 = cirq.NamedQubit('q1_0')
|
|
1700
|
+
q1_1 = cirq.NamedQubit('q1_1')
|
|
1701
|
+
q2_0 = cirq.NamedQubit('q2_0')
|
|
1702
|
+
q2_1 = cirq.NamedQubit('q2_1')
|
|
1703
|
+
q3_0 = cirq.NamedQubit('q3_0')
|
|
1704
|
+
q3_1 = cirq.NamedQubit('q3_1')
|
|
1705
|
+
q4_0 = cirq.NamedQubit('q4_0')
|
|
1706
|
+
q4_1 = cirq.NamedQubit('q4_1')
|
|
1707
|
+
|
|
1708
|
+
expected_circuit = Circuit()
|
|
1709
|
+
|
|
1710
|
+
expected_circuit.append(cirq_gate(q1_0, q1_1, q2_0, q3_0))
|
|
1711
|
+
|
|
1712
|
+
expected_circuit.append(cirq_gate(q1_0, q2_0, q3_0, q4_0))
|
|
1713
|
+
expected_circuit.append(cirq_gate(q1_1, q2_0, q3_0, q4_0))
|
|
1714
|
+
|
|
1715
|
+
expected_circuit.append(cirq_gate(q1_0, q2_0, q3_0, q4_0))
|
|
1716
|
+
expected_circuit.append(cirq_gate(q1_1, q2_1, q3_1, q4_1))
|
|
1717
|
+
|
|
1718
|
+
parsed_qasm = parser.parse(qasm)
|
|
1719
|
+
|
|
1720
|
+
assert parsed_qasm.supportedFormat
|
|
1721
|
+
assert parsed_qasm.qelib1Include
|
|
1722
|
+
|
|
1723
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1724
|
+
assert parsed_qasm.qregs == {'q1': 2, 'q2': 2, 'q3': 2, 'q4': 2}
|
|
1725
|
+
|
|
1726
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1727
|
+
|
|
1728
|
+
|
|
1729
|
+
@pytest.mark.parametrize('qasm_gate', [g[0] for g in four_qubit_gates])
|
|
1730
|
+
def test_four_qubit_gates_not_enough_args(qasm_gate: str) -> None:
|
|
1731
|
+
qasm = f"""OPENQASM 2.0;
|
|
1732
|
+
include "qelib1.inc";
|
|
1733
|
+
qreg q[2];
|
|
1734
|
+
{qasm_gate} q[0];
|
|
1735
|
+
"""
|
|
1736
|
+
|
|
1737
|
+
parser = QasmParser()
|
|
1738
|
+
|
|
1739
|
+
with pytest.raises(QasmException, match=rf".*{qasm_gate}.* takes 4 arg\(s\).*got.*1.*line 4"):
|
|
1740
|
+
parser.parse(qasm)
|
|
1741
|
+
|
|
1742
|
+
|
|
1743
|
+
@pytest.mark.parametrize('qasm_gate', [g[0] for g in four_qubit_gates])
|
|
1744
|
+
def test_four_qubit_gates_with_too_much_parameters(qasm_gate: str) -> None:
|
|
1745
|
+
qasm = f"""OPENQASM 2.0;
|
|
1746
|
+
include "qelib1.inc";
|
|
1747
|
+
qreg q[4];
|
|
1748
|
+
{qasm_gate}(pi) q[0],q[1],q[2],q[3];
|
|
1749
|
+
"""
|
|
1750
|
+
|
|
1751
|
+
parser = QasmParser()
|
|
1752
|
+
|
|
1753
|
+
with pytest.raises(QasmException, match=f".*{qasm_gate}.*parameter.*line 4.*"):
|
|
1754
|
+
parser.parse(qasm)
|
|
1755
|
+
|
|
1756
|
+
|
|
1757
|
+
five_qubit_gates = [('c4x', cirq.ControlledGate(cirq.X, num_controls=4))]
|
|
1758
|
+
|
|
1759
|
+
|
|
1760
|
+
@pytest.mark.parametrize('qasm_gate,cirq_gate', five_qubit_gates)
|
|
1761
|
+
def test_five_qubit_gates(qasm_gate: str, cirq_gate: cirq.Gate) -> None:
|
|
1762
|
+
qasm = f"""
|
|
1763
|
+
OPENQASM 2.0;
|
|
1764
|
+
include "qelib1.inc";
|
|
1765
|
+
qreg q1[2];
|
|
1766
|
+
qreg q2[2];
|
|
1767
|
+
qreg q3[2];
|
|
1768
|
+
qreg q4[2];
|
|
1769
|
+
qreg q5[2];
|
|
1770
|
+
{qasm_gate} q1[0], q1[1], q2[0], q3[0], q4[0];
|
|
1771
|
+
{qasm_gate} q1, q2[0], q3[0], q4[0], q5[0];
|
|
1772
|
+
{qasm_gate} q1, q2, q3, q4, q5;
|
|
1773
|
+
"""
|
|
1774
|
+
parser = QasmParser()
|
|
1775
|
+
|
|
1776
|
+
q1_0 = cirq.NamedQubit('q1_0')
|
|
1777
|
+
q1_1 = cirq.NamedQubit('q1_1')
|
|
1778
|
+
q2_0 = cirq.NamedQubit('q2_0')
|
|
1779
|
+
q2_1 = cirq.NamedQubit('q2_1')
|
|
1780
|
+
q3_0 = cirq.NamedQubit('q3_0')
|
|
1781
|
+
q3_1 = cirq.NamedQubit('q3_1')
|
|
1782
|
+
q4_0 = cirq.NamedQubit('q4_0')
|
|
1783
|
+
q4_1 = cirq.NamedQubit('q4_1')
|
|
1784
|
+
q5_0 = cirq.NamedQubit('q5_0')
|
|
1785
|
+
q5_1 = cirq.NamedQubit('q5_1')
|
|
1786
|
+
|
|
1787
|
+
expected_circuit = Circuit()
|
|
1788
|
+
|
|
1789
|
+
expected_circuit.append(cirq_gate(q1_0, q1_1, q2_0, q3_0, q4_0))
|
|
1790
|
+
|
|
1791
|
+
expected_circuit.append(cirq_gate(q1_0, q2_0, q3_0, q4_0, q5_0))
|
|
1792
|
+
expected_circuit.append(cirq_gate(q1_1, q2_0, q3_0, q4_0, q5_0))
|
|
1793
|
+
|
|
1794
|
+
expected_circuit.append(cirq_gate(q1_0, q2_0, q3_0, q4_0, q5_0))
|
|
1795
|
+
expected_circuit.append(cirq_gate(q1_1, q2_1, q3_1, q4_1, q5_1))
|
|
1796
|
+
|
|
1797
|
+
parsed_qasm = parser.parse(qasm)
|
|
1798
|
+
|
|
1799
|
+
assert parsed_qasm.supportedFormat
|
|
1800
|
+
assert parsed_qasm.qelib1Include
|
|
1801
|
+
|
|
1802
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1803
|
+
assert parsed_qasm.qregs == {'q1': 2, 'q2': 2, 'q3': 2, 'q4': 2, 'q5': 2}
|
|
1804
|
+
|
|
1805
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1806
|
+
|
|
1807
|
+
|
|
1808
|
+
@pytest.mark.parametrize('qasm_gate', [g[0] for g in five_qubit_gates])
|
|
1809
|
+
def test_five_qubit_gates_not_enough_args(qasm_gate: str) -> None:
|
|
1810
|
+
qasm = f"""OPENQASM 2.0;
|
|
1811
|
+
include "qelib1.inc";
|
|
1812
|
+
qreg q[2];
|
|
1813
|
+
{qasm_gate} q[0];
|
|
1814
|
+
"""
|
|
1815
|
+
|
|
1816
|
+
parser = QasmParser()
|
|
1817
|
+
|
|
1818
|
+
with pytest.raises(QasmException, match=rf".*{qasm_gate}.* takes 5 arg\(s\).*got.*1.*line 4"):
|
|
1819
|
+
parser.parse(qasm)
|
|
1820
|
+
|
|
1821
|
+
|
|
1822
|
+
@pytest.mark.parametrize('qasm_gate', [g[0] for g in five_qubit_gates])
|
|
1823
|
+
def test_five_qubit_gates_with_too_much_parameters(qasm_gate: str) -> None:
|
|
1824
|
+
qasm = f"""OPENQASM 2.0;
|
|
1825
|
+
include "qelib1.inc";
|
|
1826
|
+
qreg q[5];
|
|
1827
|
+
{qasm_gate}(pi) q[0],q[1],q[2],q[3],q[4];
|
|
1828
|
+
"""
|
|
1829
|
+
|
|
1830
|
+
parser = QasmParser()
|
|
1831
|
+
|
|
1832
|
+
with pytest.raises(QasmException, match=f".*{qasm_gate}.*parameter.*line 4.*"):
|
|
1833
|
+
parser.parse(qasm)
|
|
1834
|
+
|
|
1835
|
+
|
|
1160
1836
|
@pytest.mark.parametrize('qasm_gate,cirq_gate', single_qubit_gates)
|
|
1161
1837
|
def test_single_qubit_gates(qasm_gate: str, cirq_gate: cirq.Gate) -> None:
|
|
1162
1838
|
qasm = f"""OPENQASM 2.0;
|
|
@@ -1181,6 +1857,8 @@ def test_single_qubit_gates(qasm_gate: str, cirq_gate: cirq.Gate) -> None:
|
|
|
1181
1857
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1182
1858
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1183
1859
|
|
|
1860
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1861
|
+
|
|
1184
1862
|
|
|
1185
1863
|
def test_openqasm_3_0_qubits() -> None:
|
|
1186
1864
|
qasm = """OPENQASM 3.0;
|
|
@@ -1538,6 +2216,30 @@ def test_rzz_gate() -> None:
|
|
|
1538
2216
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1539
2217
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1540
2218
|
|
|
2219
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
2220
|
+
|
|
2221
|
+
|
|
2222
|
+
def test_ryy_gate() -> None:
|
|
2223
|
+
qasm = """
|
|
2224
|
+
OPENQASM 2.0;
|
|
2225
|
+
include "qelib1.inc";
|
|
2226
|
+
qreg q[2];
|
|
2227
|
+
ryy(pi/3) q[0],q[1];
|
|
2228
|
+
"""
|
|
2229
|
+
parser = QasmParser()
|
|
2230
|
+
|
|
2231
|
+
q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1')
|
|
2232
|
+
|
|
2233
|
+
expected_circuit = Circuit()
|
|
2234
|
+
expected_circuit.append(cirq.YYPowGate(exponent=1 / 3).on(q0, q1))
|
|
2235
|
+
|
|
2236
|
+
parsed_qasm = parser.parse(qasm)
|
|
2237
|
+
|
|
2238
|
+
assert parsed_qasm.supportedFormat
|
|
2239
|
+
assert parsed_qasm.qelib1Include
|
|
2240
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
2241
|
+
assert parsed_qasm.qregs == {'q': 2}
|
|
2242
|
+
|
|
1541
2243
|
|
|
1542
2244
|
def test_rxx_gate() -> None:
|
|
1543
2245
|
qasm = """
|
|
@@ -1561,20 +2263,22 @@ def test_rxx_gate() -> None:
|
|
|
1561
2263
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1562
2264
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1563
2265
|
|
|
2266
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1564
2267
|
|
|
1565
|
-
|
|
2268
|
+
|
|
2269
|
+
def test_crx_gate() -> None:
|
|
1566
2270
|
qasm = """
|
|
1567
2271
|
OPENQASM 2.0;
|
|
1568
2272
|
include "qelib1.inc";
|
|
1569
2273
|
qreg q[2];
|
|
1570
|
-
|
|
2274
|
+
crx(pi/7) q[0],q[1];
|
|
1571
2275
|
"""
|
|
1572
2276
|
parser = QasmParser()
|
|
1573
2277
|
|
|
1574
2278
|
q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1')
|
|
1575
2279
|
|
|
1576
2280
|
expected_circuit = Circuit()
|
|
1577
|
-
expected_circuit.append(cirq.
|
|
2281
|
+
expected_circuit.append(cirq.ControlledGate(cirq.rx(np.pi / 7)).on(q0, q1))
|
|
1578
2282
|
|
|
1579
2283
|
parsed_qasm = parser.parse(qasm)
|
|
1580
2284
|
|
|
@@ -1584,20 +2288,22 @@ def test_ryy_gate() -> None:
|
|
|
1584
2288
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1585
2289
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1586
2290
|
|
|
2291
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
1587
2292
|
|
|
1588
|
-
|
|
2293
|
+
|
|
2294
|
+
def test_cry_gate() -> None:
|
|
1589
2295
|
qasm = """
|
|
1590
2296
|
OPENQASM 2.0;
|
|
1591
2297
|
include "qelib1.inc";
|
|
1592
2298
|
qreg q[2];
|
|
1593
|
-
|
|
2299
|
+
cry(pi/7) q[0],q[1];
|
|
1594
2300
|
"""
|
|
1595
2301
|
parser = QasmParser()
|
|
1596
2302
|
|
|
1597
2303
|
q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1')
|
|
1598
2304
|
|
|
1599
2305
|
expected_circuit = Circuit()
|
|
1600
|
-
expected_circuit.append(cirq.ControlledGate(cirq.
|
|
2306
|
+
expected_circuit.append(cirq.ControlledGate(cirq.ry(np.pi / 7)).on(q0, q1))
|
|
1601
2307
|
|
|
1602
2308
|
parsed_qasm = parser.parse(qasm)
|
|
1603
2309
|
|
|
@@ -1607,6 +2313,33 @@ def test_crx_gate() -> None:
|
|
|
1607
2313
|
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
1608
2314
|
assert parsed_qasm.qregs == {'q': 2}
|
|
1609
2315
|
|
|
2316
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
def test_crz_gate() -> None:
|
|
2320
|
+
qasm = """
|
|
2321
|
+
OPENQASM 2.0;
|
|
2322
|
+
include "qelib1.inc";
|
|
2323
|
+
qreg q[2];
|
|
2324
|
+
crz(pi/7) q[0],q[1];
|
|
2325
|
+
"""
|
|
2326
|
+
parser = QasmParser()
|
|
2327
|
+
|
|
2328
|
+
q0, q1 = cirq.NamedQubit('q_0'), cirq.NamedQubit('q_1')
|
|
2329
|
+
|
|
2330
|
+
expected_circuit = Circuit()
|
|
2331
|
+
expected_circuit.append(cirq.ControlledGate(cirq.rz(np.pi / 7)).on(q0, q1))
|
|
2332
|
+
|
|
2333
|
+
parsed_qasm = parser.parse(qasm)
|
|
2334
|
+
|
|
2335
|
+
assert parsed_qasm.supportedFormat
|
|
2336
|
+
assert parsed_qasm.qelib1Include
|
|
2337
|
+
|
|
2338
|
+
ct.assert_same_circuits(parsed_qasm.circuit, expected_circuit)
|
|
2339
|
+
assert parsed_qasm.qregs == {'q': 2}
|
|
2340
|
+
|
|
2341
|
+
cq.assert_qiskit_parsed_qasm_consistent_with_unitary(qasm, cirq.unitary(expected_circuit))
|
|
2342
|
+
|
|
1610
2343
|
|
|
1611
2344
|
def test_iswap_gate() -> None:
|
|
1612
2345
|
qasm = """
|