def test_assert_deprecated_log_handling(): # correct deprecation message with assert_deprecated("hello", deadline="v1.2"): warnings.warn("hello, this is deprecated in v1.2") # missed deprecation warning with pytest.raises(AssertionError, match="Expected 1 log message but got 0."): with assert_deprecated(deadline="v1.2"): pass # too many deprecation warnings (only 1 should be emitted!) with pytest.raises(AssertionError, match="Expected 1 log message but got 2."): with assert_deprecated(deadline="v1.2"): warnings.warn("hello, this is deprecated in v1.2") warnings.warn("hello, this is deprecated in v1.2") # allowing for multiple deprecation warnings (in case of json serialization for multiple objects # for example) with assert_deprecated(deadline="v1.2", count=2): warnings.warn("hello, this is deprecated in v1.2") warnings.warn("hello, this is deprecated in v1.2") with assert_deprecated(deadline="v1.2", count=None): warnings.warn("hello, this is deprecated in v1.2") warnings.warn("hello, this is deprecated in v1.2") warnings.warn("hello, this is deprecated in v1.2")
def test_deprecated_fields(): serializer = cg.GateOpSerializer( gate_type=GateWithAttribute, serialized_gate_id='my_gate', args=[], ) with assert_deprecated('Use serialized_id', deadline='v0.13'): assert serializer.serialized_gate_id == serializer.serialized_id with assert_deprecated('Use internal_type', deadline='v0.13'): assert serializer.gate_type == serializer.internal_type
def test_deprecated_methods(): with assert_deprecated('Use supported_internal_types', deadline='v0.13'): _ = MY_GATE_SET.supported_gate_types() with assert_deprecated('Use with_added_types', deadline='v0.13'): _ = MY_GATE_SET.with_added_gates() with assert_deprecated('Use use_constants', deadline='v0.13'): # pylint: disable=unexpected-keyword-arg _ = MY_GATE_SET.serialize(cirq.Circuit(), use_constants_table_for_tokens=True)
def test_supports_on_each_inheritance_shim(): class NotOnEach(cirq.Gate): def num_qubits(self): return 1 # coverage: ignore class OnEach(cirq.ops.gate_features.SupportsOnEachGate): def num_qubits(self): return 1 # coverage: ignore class SingleQ(cirq.SingleQubitGate): pass class TwoQ(cirq.TwoQubitGate): pass not_on_each = NotOnEach() single_q = SingleQ() two_q = TwoQ() with assert_deprecated(deadline="v0.14"): on_each = OnEach() assert not isinstance(not_on_each, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(on_each, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(single_q, cirq.ops.gate_features.SupportsOnEachGate) assert not isinstance(two_q, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(cirq.X, cirq.ops.gate_features.SupportsOnEachGate) assert not isinstance(cirq.CX, cirq.ops.gate_features.SupportsOnEachGate) assert isinstance(cirq.DepolarizingChannel(0.01), cirq.ops.gate_features.SupportsOnEachGate)
def test_two_qubit_gate_is_abstract_can_implement(): class Included(cirq.TwoQubitGate): def matrix(self): pass with assert_deprecated(deadline="v0.14", count=2): assert isinstance(Included(), cirq.TwoQubitGate)
def test_supports_three_qubit_inheritance_shim(): print() class Dummy1(cirq.Gate): def num_qubits(self): return 1 class Dummy1a(cirq.SingleQubitGate): pass class Dummy3(cirq.Gate): def num_qubits(self): return 3 class Dummy3a(cirq.ThreeQubitGate): pass class NottaGate: def _num_qubits_(self): return 3 # coverage: ignore g1 = Dummy1() g1a = Dummy1a() g3 = Dummy3() with assert_deprecated(deadline="v0.14"): g3a = Dummy3a() assert not isinstance(g1, cirq.ThreeQubitGate) assert not isinstance(g1a, cirq.ThreeQubitGate) assert isinstance(g3, cirq.ThreeQubitGate) assert isinstance(g3a, cirq.ThreeQubitGate) assert not isinstance(cirq.X, cirq.ThreeQubitGate) assert isinstance(cirq.CCX, cirq.ThreeQubitGate) assert not isinstance(NottaGate(), cirq.ThreeQubitGate)
def test_supports_two_qubit_inheritance_shim(): print() class Dummy1(cirq.Gate): def num_qubits(self): return 1 class Dummy1a(cirq.SingleQubitGate): pass class Dummy2(cirq.Gate): def num_qubits(self): return 2 class Dummy2a(cirq.TwoQubitGate): pass class NottaGate: def _num_qubits_(self): return 2 # coverage: ignore g1 = Dummy1() g1a = Dummy1a() g2 = Dummy2() with assert_deprecated(deadline="v0.14"): g2a = Dummy2a() assert not isinstance(g1, cirq.TwoQubitGate) assert not isinstance(g1a, cirq.TwoQubitGate) assert isinstance(g2, cirq.TwoQubitGate) assert isinstance(g2a, cirq.TwoQubitGate) assert not isinstance(cirq.X, cirq.TwoQubitGate) assert isinstance(cirq.CX, cirq.TwoQubitGate) assert not isinstance(NottaGate(), cirq.TwoQubitGate)
def test_supports_on_each_deprecation(): class CustomGate(cirq.ops.gate_features.SupportsOnEachGate): def num_qubits(self): return 1 # coverage: ignore with assert_deprecated(deadline="v0.14"): assert isinstance(CustomGate(), cirq.ops.gate_features.SupportsOnEachGate)
def test_deprecated_fields(): deserializer = cg.GateOpDeserializer( serialized_gate_id='my_gate', gate_constructor=GateWithAttribute, args=[], ) with assert_deprecated('Use serialized_id', deadline='v0.13'): assert deserializer.serialized_gate_id == deserializer.serialized_id
def test_deprecated(): # allow_multiple_warnings is now deprecated...so this is a bit convoluted, # a parameter of the deprecator is being deprecated with assert_deprecated(deadline="v0.12", count=3): with pytest.raises(AssertionError, match="Expected 1 log message but got 2."): # pylint: disable=unexpected-keyword-arg with assert_deprecated(deadline="v1.2", allow_multiple_warnings=False): warnings.warn("hello, this is deprecated in v1.2") warnings.warn("hello, this is deprecated in v1.2") with assert_deprecated(deadline="v0.12", count=3): # pylint: disable=unexpected-keyword-arg with assert_deprecated(deadline="v1.2", allow_multiple_warnings=True): warnings.warn("hello, this is deprecated in v1.2") warnings.warn("hello, this is deprecated in v1.2")
def test_single_qubit_gate_validate_args(): class Dummy(cirq.SingleQubitGate): def matrix(self): pass with assert_deprecated(deadline="v1.0"): g = Dummy() with assert_deprecated(deadline="isinstance(gate, SingleQubitGate) is deprecated"): assert isinstance(g, cirq.SingleQubitGate) q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') assert g.num_qubits() == 1 g.validate_args([q1]) g.validate_args([q2]) with pytest.raises(ValueError): g.validate_args([]) with pytest.raises(ValueError): g.validate_args([q1, q2])
def test_two_qubit_gate_validate_pass(): class Dummy(cirq.TwoQubitGate): def matrix(self): pass with assert_deprecated(deadline="v0.14"): g = Dummy() q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') q3 = cirq.NamedQubit('q3') assert g.num_qubits() == 2 g.validate_args([q1, q2]) g.validate_args([q2, q3]) g.validate_args([q3, q2])
def test_two_qubit_gate_validate_wrong_number(): class Dummy(cirq.TwoQubitGate): def matrix(self): pass with assert_deprecated(deadline="v0.14"): g = Dummy() q1 = cirq.NamedQubit('q1') q2 = cirq.NamedQubit('q2') q3 = cirq.NamedQubit('q3') with pytest.raises(ValueError): g.validate_args([]) with pytest.raises(ValueError): g.validate_args([q1]) with pytest.raises(ValueError): g.validate_args([q1, q2, q3])
def test_three_qubit_gate_validate(): class Dummy(cirq.ThreeQubitGate): def matrix(self): pass with assert_deprecated(deadline="v0.14"): g = Dummy() a, b, c, d = cirq.LineQubit.range(4) assert g.num_qubits() == 3 g.validate_args([a, b, c]) with pytest.raises(ValueError): g.validate_args([]) with pytest.raises(ValueError): g.validate_args([a]) with pytest.raises(ValueError): g.validate_args([a, b]) with pytest.raises(ValueError): g.validate_args([a, b, c, d])
def test_nested_assert_deprecation(): with assert_deprecated(deadline="v1.2", count=1): with assert_deprecated(deadline="v1.2", count=1): with assert_deprecated(deadline="v1.2", count=1): warnings.warn("hello, this is deprecated in v1.2")