11# pylint: disable=wrong-or-nonexistent-copyright-notice
22import os
3+ import warnings
34from unittest .mock import patch , PropertyMock
45from math import sqrt
56import pathlib
1213 UnsupportedQubit ,
1314 UnsupportedRigettiQCSOperation ,
1415)
16+ from cirq_rigetti .deprecation import allow_deprecated_cirq_rigetti_use_in_tests
1517from qcs_sdk .qpu .isa import InstructionSetArchitecture , Family
1618import numpy as np
19+ from cirq ._compat import ALLOW_DEPRECATION_IN_TEST
20+ from cirq_rigetti .deprecation import allow_deprecated_cirq_rigetti_use_in_tests
1721
1822dir_path = pathlib .Path (os .path .dirname (os .path .realpath (__file__ )))
1923fixture_path = dir_path / '__fixtures__'
2024
25+ # test parameterization uses deprecated classes, therefore we need to have
26+ # ALLOW_DEPRECATION_IN_TEST set during import time. The initial environment
27+ # is restored at the end of the module.
28+
29+ _SAVE_ENVIRON = {k : os .environ [k ] for k in [ALLOW_DEPRECATION_IN_TEST ] if k in os .environ }
30+ os .environ [ALLOW_DEPRECATION_IN_TEST ] = "True"
31+
32+ warnings .filterwarnings (
33+ "ignore" ,
34+ message = "(.|\n )*Cirq-Rigetti is deprecated." ,
35+ category = DeprecationWarning ,
36+ module = __name__ ,
37+ )
38+
2139
2240@pytest .fixture
2341def qcs_aspen8_isa () -> InstructionSetArchitecture :
2442 with open (fixture_path / 'QCS-Aspen-8-ISA.json' , 'r' ) as f :
2543 return InstructionSetArchitecture .from_raw (f .read ())
2644
2745
46+ @allow_deprecated_cirq_rigetti_use_in_tests
2847def test_octagonal_qubit_index ():
2948 """test that OctagonalQubit properly calculates index and uses it for comparison"""
3049 qubit0 = OctagonalQubit (0 )
3150 assert qubit0 .index == 0
3251 assert OctagonalQubit (1 ) > qubit0
3352
3453
54+ @allow_deprecated_cirq_rigetti_use_in_tests
3555def test_octagonal_qubit_repr ():
3656 """test OctagonalQubit.__repr__"""
3757 qubit5 = OctagonalQubit (5 )
3858 assert "cirq_rigetti.OctagonalQubit(octagon_position=5)" == repr (qubit5 )
3959
4060
61+ @allow_deprecated_cirq_rigetti_use_in_tests
4162def test_octagonal_qubit_positions ():
4263 """test OctagonalQubit 2D position and distance calculations"""
4364 qubit0 = OctagonalQubit (0 )
@@ -77,25 +98,29 @@ def test_octagonal_qubit_positions():
7798 _ = qubit0 .distance (AspenQubit (0 , 0 ))
7899
79100
101+ @allow_deprecated_cirq_rigetti_use_in_tests
80102def test_octagonal_position_validation ():
81103 """test OctagonalQubit validates octagon position when initialized"""
82104 with pytest .raises (ValueError ):
83105 _ = OctagonalQubit (8 )
84106
85107
108+ @allow_deprecated_cirq_rigetti_use_in_tests
86109def test_aspen_qubit_index ():
87110 """test that AspenQubit properly calculates index and uses it for comparison"""
88111 qubit10 = AspenQubit (1 , 0 )
89112 assert qubit10 .index == 10
90113 assert qubit10 > AspenQubit (0 , 5 )
91114
92115
116+ @allow_deprecated_cirq_rigetti_use_in_tests
93117def test_aspen_qubit_repr ():
94118 """test AspenQubit.__repr__"""
95119 qubit10 = AspenQubit (1 , 0 )
96120 assert "cirq_rigetti.AspenQubit(octagon=1, octagon_position=0)" == repr (qubit10 )
97121
98122
123+ @allow_deprecated_cirq_rigetti_use_in_tests
99124def test_aspen_qubit_positions_and_distance ():
100125 """test AspenQubit 2D position and distance calculations"""
101126 qubit10 = AspenQubit (1 , 0 )
@@ -131,6 +156,7 @@ def test_aspen_qubit_positions_and_distance():
131156 _ = AspenQubit (1 , 9 )
132157
133158
159+ @allow_deprecated_cirq_rigetti_use_in_tests
134160def test_aspen_qubit_qid_conversions ():
135161 """test AspenQubit conversion to and from other `cirq.Qid` implementations"""
136162 qubit10 = AspenQubit (1 , 0 )
@@ -152,6 +178,7 @@ def test_aspen_qubit_qid_conversions():
152178 _ = AspenQubit .from_grid_qubit (cirq .GridQubit (3 , 4 ))
153179
154180
181+ @allow_deprecated_cirq_rigetti_use_in_tests
155182def test_rigetti_qcs_aspen_device_topology (qcs_aspen8_isa : InstructionSetArchitecture ):
156183 """test RigettiQCSAspenDevice topological nodes and edges"""
157184 device = RigettiQCSAspenDevice (isa = qcs_aspen8_isa )
@@ -171,6 +198,7 @@ def test_rigetti_qcs_aspen_device_topology(qcs_aspen8_isa: InstructionSetArchite
171198 OctagonalQubit (6 ),
172199 ],
173200)
201+ @allow_deprecated_cirq_rigetti_use_in_tests
174202def test_rigetti_qcs_aspen_device_valid_qubit (
175203 qubit : cirq .Qid , qcs_aspen8_isa : InstructionSetArchitecture
176204):
@@ -191,6 +219,7 @@ def test_rigetti_qcs_aspen_device_valid_qubit(
191219 AspenQubit (4 , 0 ),
192220 ],
193221)
222+ @allow_deprecated_cirq_rigetti_use_in_tests
194223def test_rigetti_qcs_aspen_device_invalid_qubit (
195224 qubit : cirq .Qid , qcs_aspen8_isa : InstructionSetArchitecture
196225):
@@ -212,6 +241,7 @@ def test_rigetti_qcs_aspen_device_invalid_qubit(
212241 cirq .CNOT (AspenQubit (0 , 1 ), AspenQubit (1 , 1 )),
213242 ],
214243)
244+ @allow_deprecated_cirq_rigetti_use_in_tests
215245def test_rigetti_qcs_aspen_device_invalid_operation (
216246 operation : cirq .Operation , qcs_aspen8_isa : InstructionSetArchitecture
217247):
@@ -224,6 +254,7 @@ def test_rigetti_qcs_aspen_device_invalid_operation(
224254
225255
226256@pytest .mark .parametrize ('operation' , [cirq .CNOT (AspenQubit (0 , 1 ), AspenQubit (0 , 2 ))])
257+ @allow_deprecated_cirq_rigetti_use_in_tests
227258def test_rigetti_qcs_aspen_device_valid_operation (
228259 operation : cirq .Operation , qcs_aspen8_isa : InstructionSetArchitecture
229260):
@@ -234,6 +265,7 @@ def test_rigetti_qcs_aspen_device_valid_operation(
234265 device .validate_operation (operation )
235266
236267
268+ @allow_deprecated_cirq_rigetti_use_in_tests
237269def test_rigetti_qcs_aspen_device_qubits (qcs_aspen8_isa : InstructionSetArchitecture ):
238270 """test RigettiQCSAspenDevice returns accurate set of qubits"""
239271 device = RigettiQCSAspenDevice (isa = qcs_aspen8_isa )
@@ -244,12 +276,14 @@ def test_rigetti_qcs_aspen_device_qubits(qcs_aspen8_isa: InstructionSetArchitect
244276 assert expected_qubits == set (device .qubits ())
245277
246278
279+ @allow_deprecated_cirq_rigetti_use_in_tests
247280def test_rigetti_qcs_aspen_device_repr (qcs_aspen8_isa : InstructionSetArchitecture ):
248281 """test RigettiQCSAspenDevice.__repr__"""
249282 device = RigettiQCSAspenDevice (isa = qcs_aspen8_isa )
250283 assert f'cirq_rigetti.RigettiQCSAspenDevice(isa={ qcs_aspen8_isa !r} )' == repr (device )
251284
252285
286+ @allow_deprecated_cirq_rigetti_use_in_tests
253287def test_rigetti_qcs_aspen_device_family_validation (qcs_aspen8_isa : InstructionSetArchitecture ):
254288 """test RigettiQCSAspenDevice validates architecture family on initialization"""
255289 non_aspen_isa = InstructionSetArchitecture .from_raw (qcs_aspen8_isa .json ())
@@ -260,10 +294,16 @@ def test_rigetti_qcs_aspen_device_family_validation(qcs_aspen8_isa: InstructionS
260294 ), 'ISA family is read-only and should still be Aspen'
261295
262296
297+ @allow_deprecated_cirq_rigetti_use_in_tests
263298def test_get_rigetti_qcs_aspen_device (qcs_aspen8_isa : InstructionSetArchitecture ):
264299 with patch ('cirq_rigetti.aspen_device.get_instruction_set_architecture' ) as mock :
265300 mock .return_value = qcs_aspen8_isa
266301
267302 from cirq_rigetti .aspen_device import get_rigetti_qcs_aspen_device
268303
269304 assert get_rigetti_qcs_aspen_device ('Aspen-8' ) == RigettiQCSAspenDevice (isa = qcs_aspen8_isa )
305+
306+
307+ # clean up extra environment variable
308+ del os .environ [ALLOW_DEPRECATION_IN_TEST ]
309+ os .environ .update (_SAVE_ENVIRON )
0 commit comments