Skip to content

Commit

Permalink
Rename TestDevice -> FakeDevice to avoid pytest confusion (#5066)
Browse files Browse the repository at this point in the history
Pytest tries to collect `TestDevice` as a test class, and fails with the following wraning:
```
cirq-core/cirq/circuits/circuit_dag_test.py:24
cirq-core/cirq/circuits/circuit_dag_test.py:24
  /home/runner/work/Cirq/Cirq/cirq-core/cirq/circuits/circuit_dag_test.py:24: PytestCollectionWarning: cannot collect test class 'TestDevice' because it has a __init__ constructor (from: cirq-core/cirq/circuits/circuit_dag_test.py)
    class TestDevice(cirq.Device):
```
  • Loading branch information
maffoo authored Mar 22, 2022
1 parent 45624ff commit 33580bb
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 30 deletions.
11 changes: 5 additions & 6 deletions cirq-core/cirq/circuits/circuit_dag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import cirq


class TestDevice(cirq.Device):
def __init__(self):
pass
class FakeDevice(cirq.Device):
pass


def test_wrapper_eq():
Expand Down Expand Up @@ -124,7 +123,7 @@ def test_from_ops_device_deprecated():
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
q0 = cirq.LineQubit(0)
_ = cirq.CircuitDag.from_ops(cirq.X(q0), cirq.Y(q0), device=TestDevice())
_ = cirq.CircuitDag.from_ops(cirq.X(q0), cirq.Y(q0), device=FakeDevice())


def test_from_circuit():
Expand All @@ -140,7 +139,7 @@ def test_from_circuit():
def test_from_circuit_deprecated():
q0 = cirq.LineQubit(0)
circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q0))
circuit._device = TestDevice()
circuit._device = FakeDevice()
with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
Expand Down Expand Up @@ -172,7 +171,7 @@ def test_to_circuit_device_deprecated():
q0 = cirq.LineQubit(0)
circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q0))
dag = cirq.CircuitDag.from_circuit(circuit)
dag._device = TestDevice()
dag._device = FakeDevice()

with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ def test_approx_eq(circuit_cls):

@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_approx_eq_device_deprecated(circuit_cls):
class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def validate_operation(self, operation: cirq.Operation) -> None:
pass

a = cirq.NamedQubit('a')
with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
other_device = circuit_cls([cirq.Moment([cirq.X(a)])], device=TestDevice())
other_device = circuit_cls([cirq.Moment([cirq.X(a)])], device=FakeDevice())
assert not cirq.approx_eq(
circuit_cls([cirq.Moment([cirq.X(a)])]),
other_device,
Expand Down
16 changes: 8 additions & 8 deletions cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from cirq.contrib.quantum_volume import CompilationResult


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
qubits = cirq.GridQubit.rect(5, 5)


Expand Down Expand Up @@ -123,7 +123,7 @@ def test_compile_circuit_router():
router_mock = MagicMock()
cirq.contrib.quantum_volume.compile_circuit(
cirq.Circuit(),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
router=router_mock,
routing_attempts=1,
)
Expand All @@ -141,15 +141,15 @@ def test_compile_circuit():
)
compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
compiler=compiler_mock,
routing_attempts=1,
)

assert len(compilation_result.mapping) == 3
assert cirq.contrib.routing.ops_are_consistent_with_device_graph(
compilation_result.circuit.all_operations(),
cirq.contrib.routing.gridqubits_to_graph_device(TestDevice().qubits),
cirq.contrib.routing.gridqubits_to_graph_device(FakeDevice().qubits),
)
compiler_mock.assert_called_with(compilation_result.circuit)

Expand All @@ -169,7 +169,7 @@ def test_compile_circuit_replaces_swaps():
)
compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
compiler=compiler_mock,
routing_attempts=1,
)
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_compile_circuit_with_readout_correction():
)
compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
compiler=compiler_mock,
router=router_mock,
routing_attempts=1,
Expand Down Expand Up @@ -258,7 +258,7 @@ def test_compile_circuit_multiple_routing_attempts():

compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
compiler=compiler_mock,
router=router_mock,
routing_attempts=3,
Expand All @@ -281,7 +281,7 @@ def test_compile_circuit_no_routing_attempts():
with pytest.raises(AssertionError) as e:
cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
device_graph=ccr.gridqubits_to_graph_device(FakeDevice().qubits),
routing_attempts=0,
)
assert e.match('Unable to get routing for circuit')
Expand Down
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/line/placement/anneal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
from cirq_google.line.placement.chip import chip_as_adjacency_list


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self, qubits):
self.qubits = qubits


def _create_device(qubits: Iterable[cirq.GridQubit]):
return TestDevice(qubits)
return FakeDevice(qubits)


@mock.patch('cirq_google.line.placement.optimization.anneal_minimize')
Expand Down
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/line/placement/chip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ def test_qubit_not_mutated():
assert qubit == cirq.GridQubit(0, 0)


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self, qubits):
self.qubits = qubits


def _create_device(qubits: Iterable[cirq.GridQubit]):
return TestDevice(qubits)
return FakeDevice(qubits)


def test_empty():
Expand Down
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/line/placement/greedy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
)


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self, qubits):
self.qubits = qubits


def _create_device(qubits: Iterable[cirq.GridQubit]):
return TestDevice(qubits)
return FakeDevice(qubits)


def test_greedy_sequence_search_fails_on_wrong_start_qubit():
Expand Down
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/line/placement/line_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from cirq_google.line.placement import GridQubitLineTuple


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self, qubits):
self.qubits = qubits

Expand All @@ -28,7 +28,7 @@ def test_anneal_method_calls_anneal_search():
q00 = cirq.GridQubit(0, 0)
q01 = cirq.GridQubit(0, 1)
q03 = cirq.GridQubit(0, 3)
device = TestDevice(qubits=[q00, q01, q03])
device = FakeDevice(qubits=[q00, q01, q03])
length = 2
method = cg.AnnealSequenceSearchStrategy()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import cirq_google as cg


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self):
pass

Expand Down Expand Up @@ -132,7 +132,7 @@ def test_assert_new_device_deprecated():
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
_ = cg.optimized_for_sycamore(
circuit0, optimizer_type='sqrt_iswap', new_device=TestDevice()
circuit0, optimizer_type='sqrt_iswap', new_device=FakeDevice()
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from cirq_google.api import v2


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self):
pass

Expand Down Expand Up @@ -680,7 +680,7 @@ def test_deserialize_schedule_not_supported():
),
)
with pytest.raises(ValueError, match='no longer supported'):
serializer.deserialize(proto, TestDevice())
serializer.deserialize(proto, FakeDevice())


def test_deserialize_fsim_missing_parameters():
Expand Down
4 changes: 2 additions & 2 deletions cirq-google/cirq_google/workflow/qubit_placement_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np


class TestDevice(cirq.Device):
class FakeDevice(cirq.Device):
def __init__(self):
self.qubits = cirq.GridQubit.rect(2, 8)
neighbors = [(a, b) for a in self.qubits for b in self.qubits if a.is_adjacent(b)]
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_random_device_placer_small_device():
qp.place_circuit(
circuit,
problem_topology=topo,
shared_rt_info=cg.SharedRuntimeInfo(run_id='1', device=TestDevice()),
shared_rt_info=cg.SharedRuntimeInfo(run_id='1', device=FakeDevice()),
rs=np.random.RandomState(1),
)

Expand Down

0 comments on commit 33580bb

Please sign in to comment.