@@ -38,10 +38,14 @@ def test_optimizer_output_gates_are_supported(optimizer_type, gateset):
38
38
circuit = cirq .Circuit (
39
39
cirq .CZ (q0 , q1 ), cirq .X (q0 ) ** 0.2 , cirq .Z (q1 ) ** 0.2 , cirq .measure (q0 , q1 , key = 'm' )
40
40
)
41
- new_circuit = cg .optimized_for_sycamore (circuit , optimizer_type = optimizer_type )
42
- for moment in new_circuit :
43
- for op in moment :
44
- assert gateset .is_supported_operation (op )
41
+
42
+ with cirq .testing .assert_deprecated (
43
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 1
44
+ ):
45
+ new_circuit = cg .optimized_for_sycamore (circuit , optimizer_type = optimizer_type )
46
+ for moment in new_circuit :
47
+ for op in moment :
48
+ assert gateset .is_supported_operation (op )
45
49
46
50
47
51
@pytest .mark .parametrize ('optimizer_type, gateset' , _OPTIMIZERS_AND_GATESETS )
@@ -53,19 +57,26 @@ def test_optimize_large_measurement_gates(optimizer_type, gateset):
53
57
[cirq .CZ (qubits [i ], qubits [i + 1 ]) for i in range (1 , len (qubits ) - 1 , 2 )],
54
58
cirq .measure (* qubits , key = 'm' ),
55
59
)
56
- new_circuit = cg .optimized_for_sycamore (circuit , optimizer_type = optimizer_type )
57
- for moment in new_circuit :
58
- for op in moment :
59
- assert gateset .is_supported_operation (op )
60
+
61
+ with cirq .testing .assert_deprecated (
62
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 1
63
+ ):
64
+ new_circuit = cg .optimized_for_sycamore (circuit , optimizer_type = optimizer_type )
65
+ for moment in new_circuit :
66
+ for op in moment :
67
+ assert gateset .is_supported_operation (op )
60
68
61
69
62
70
def test_invalid_input ():
63
- with pytest .raises (ValueError ):
64
- q0 , q1 = cirq .LineQubit .range (2 )
65
- circuit = cirq .Circuit (
66
- cirq .CZ (q0 , q1 ), cirq .X (q0 ) ** 0.2 , cirq .Z (q1 ) ** 0.2 , cirq .measure (q0 , q1 , key = 'm' )
67
- )
68
- _ = cg .optimized_for_sycamore (circuit , optimizer_type = 'for_tis_100' )
71
+ with cirq .testing .assert_deprecated (
72
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 1
73
+ ):
74
+ with pytest .raises (ValueError ):
75
+ q0 , q1 = cirq .LineQubit .range (2 )
76
+ circuit = cirq .Circuit (
77
+ cirq .CZ (q0 , q1 ), cirq .X (q0 ) ** 0.2 , cirq .Z (q1 ) ** 0.2 , cirq .measure (q0 , q1 , key = 'm' )
78
+ )
79
+ _ = cg .optimized_for_sycamore (circuit , optimizer_type = 'for_tis_100' )
69
80
70
81
71
82
def test_tabulation ():
@@ -74,53 +85,67 @@ def test_tabulation():
74
85
circuit = cirq .Circuit (cirq .MatrixGate (u ).on (q0 , q1 ))
75
86
np .testing .assert_allclose (u , cirq .unitary (circuit ))
76
87
77
- circuit2 = cg .optimized_for_sycamore (circuit , optimizer_type = 'sycamore' )
78
- cirq .testing .assert_allclose_up_to_global_phase (u , cirq .unitary (circuit2 ), atol = 1e-5 )
79
- assert len (circuit2 ) == 13
80
-
81
- # Note this is run on every commit, so it needs to be relatively quick.
82
- # This requires us to use relatively loose tolerances
83
- circuit3 = cg .optimized_for_sycamore (
84
- circuit , optimizer_type = 'sycamore' , tabulation_resolution = 0.1
85
- )
86
- cirq .testing .assert_allclose_up_to_global_phase (u , cirq .unitary (circuit3 ), rtol = 1e-1 , atol = 1e-1 )
87
- assert len (circuit3 ) == 7
88
+ with cirq .testing .assert_deprecated (
89
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 2
90
+ ):
91
+ circuit2 = cg .optimized_for_sycamore (circuit , optimizer_type = 'sycamore' )
92
+ cirq .testing .assert_allclose_up_to_global_phase (u , cirq .unitary (circuit2 ), atol = 1e-5 )
93
+ assert len (circuit2 ) == 13
94
+ # Note this is run on every commit, so it needs to be relatively quick.
95
+ # This requires us to use relatively loose tolerances
96
+ circuit3 = cg .optimized_for_sycamore (
97
+ circuit , optimizer_type = 'sycamore' , tabulation_resolution = 0.1
98
+ )
99
+ cirq .testing .assert_allclose_up_to_global_phase (
100
+ u , cirq .unitary (circuit3 ), rtol = 1e-1 , atol = 1e-1
101
+ )
102
+ assert len (circuit3 ) == 7
88
103
89
104
90
105
def test_no_tabulation ():
91
106
circuit = cirq .Circuit (cirq .X (cirq .LineQubit (0 )))
92
- with pytest .raises (NotImplementedError ):
93
- cg .optimized_for_sycamore (circuit , optimizer_type = 'sqrt_iswap' , tabulation_resolution = 0.01 )
94
107
95
- with pytest .raises (NotImplementedError ):
96
- cg .optimized_for_sycamore (circuit , optimizer_type = 'xmon' , tabulation_resolution = 0.01 )
108
+ with cirq .testing .assert_deprecated (
109
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 3
110
+ ):
111
+ with pytest .raises (NotImplementedError ):
112
+ cg .optimized_for_sycamore (
113
+ circuit , optimizer_type = 'sqrt_iswap' , tabulation_resolution = 0.01
114
+ )
97
115
98
- with pytest .raises (NotImplementedError ):
99
- cg .optimized_for_sycamore (
100
- circuit , optimizer_type = 'xmon_partial_cz' , tabulation_resolution = 0.01
101
- )
116
+ with pytest .raises (NotImplementedError ):
117
+ cg .optimized_for_sycamore (circuit , optimizer_type = 'xmon' , tabulation_resolution = 0.01 )
118
+
119
+ with pytest .raises (NotImplementedError ):
120
+ cg .optimized_for_sycamore (
121
+ circuit , optimizer_type = 'xmon_partial_cz' , tabulation_resolution = 0.01
122
+ )
102
123
103
124
104
125
def test_one_q_matrix_gate ():
105
126
u = cirq .testing .random_special_unitary (2 )
106
127
q = cirq .LineQubit (0 )
107
128
circuit0 = cirq .Circuit (cirq .MatrixGate (u ).on (q ))
108
129
assert len (circuit0 ) == 1
109
- circuit_iswap = cg .optimized_for_sycamore (circuit0 , optimizer_type = 'sqrt_iswap' )
110
- assert len (circuit_iswap ) == 1
111
- for moment in circuit_iswap :
112
- for op in moment :
113
- assert cg .SQRT_ISWAP_GATESET .is_supported_operation (op )
114
- # single qubit gates shared between gatesets, so:
115
- assert cg .SYC_GATESET .is_supported_operation (op )
116
-
117
- circuit_syc = cg .optimized_for_sycamore (circuit0 , optimizer_type = 'sycamore' )
118
- assert len (circuit_syc ) == 1
119
- for moment in circuit_iswap :
120
- for op in moment :
121
- assert cg .SYC_GATESET .is_supported_operation (op )
122
- # single qubit gates shared between gatesets, so:
123
- assert cg .SQRT_ISWAP_GATESET .is_supported_operation (op )
130
+
131
+ with cirq .testing .assert_deprecated (
132
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 2
133
+ ):
134
+ circuit_iswap = cg .optimized_for_sycamore (circuit0 , optimizer_type = 'sqrt_iswap' )
135
+ assert len (circuit_iswap ) == 1
136
+ for moment in circuit_iswap :
137
+ for op in moment :
138
+ assert cg .SQRT_ISWAP_GATESET .is_supported_operation (op )
139
+ # single qubit gates shared between gatesets, so:
140
+ assert cg .SYC_GATESET .is_supported_operation (op )
141
+
142
+ circuit_syc = cg .optimized_for_sycamore (circuit0 , optimizer_type = 'sycamore' )
143
+ assert len (circuit_syc ) == 1
144
+ for moment in circuit_iswap :
145
+ for op in moment :
146
+ assert cg .SYC_GATESET .is_supported_operation (op )
147
+ # single qubit gates shared between gatesets, so:
148
+ assert cg .SQRT_ISWAP_GATESET .is_supported_operation (op )
124
149
125
150
126
151
@pytest .mark .parametrize (
@@ -131,20 +156,23 @@ def test_circuit_operation_conversion(optimizer_type, two_qubit_gate_type):
131
156
q0 , q1 = cirq .LineQubit .range (2 )
132
157
subcircuit = cirq .FrozenCircuit (cirq .X (q0 ), cirq .SWAP (q0 , q1 ))
133
158
circuit = cirq .Circuit (cirq .CircuitOperation (subcircuit ))
134
- converted_circuit = cg .optimized_for_sycamore (circuit , optimizer_type = optimizer_type )
135
- # Verify that the CircuitOperation was preserved.
136
- ops = list (converted_circuit .all_operations ())
137
- assert isinstance (ops [0 ], cirq .CircuitOperation )
138
- # Verify that the contents of the CircuitOperation were optimized.
139
- converted_subcircuit = cg .optimized_for_sycamore (
140
- subcircuit .unfreeze (), optimizer_type = optimizer_type
141
- )
142
- assert len (
143
- [* converted_subcircuit .findall_operations_with_gate_type (two_qubit_gate_type )]
144
- ) == len ([* ops [0 ].circuit .findall_operations_with_gate_type (two_qubit_gate_type )])
145
- cirq .testing .assert_circuits_with_terminal_measurements_are_equivalent (
146
- ops [0 ].circuit , converted_subcircuit , atol = 1e-6
147
- )
148
- cirq .testing .assert_circuits_with_terminal_measurements_are_equivalent (
149
- circuit , converted_circuit , atol = 1e-6
150
- )
159
+ with cirq .testing .assert_deprecated (
160
+ 'Use `cirq.optimize_for_target_gateset' , deadline = 'v0.16' , count = 2
161
+ ):
162
+ converted_circuit = cg .optimized_for_sycamore (circuit , optimizer_type = optimizer_type )
163
+ # Verify that the CircuitOperation was preserved.
164
+ ops = list (converted_circuit .all_operations ())
165
+ assert isinstance (ops [0 ], cirq .CircuitOperation )
166
+ # Verify that the contents of the CircuitOperation were optimized.
167
+ converted_subcircuit = cg .optimized_for_sycamore (
168
+ subcircuit .unfreeze (), optimizer_type = optimizer_type
169
+ )
170
+ assert len (
171
+ [* converted_subcircuit .findall_operations_with_gate_type (two_qubit_gate_type )]
172
+ ) == len ([* ops [0 ].circuit .findall_operations_with_gate_type (two_qubit_gate_type )])
173
+ cirq .testing .assert_circuits_with_terminal_measurements_are_equivalent (
174
+ ops [0 ].circuit , converted_subcircuit , atol = 1e-6
175
+ )
176
+ cirq .testing .assert_circuits_with_terminal_measurements_are_equivalent (
177
+ circuit , converted_circuit , atol = 1e-6
178
+ )
0 commit comments