Skip to content

Commit

Permalink
Remove dummy from cirq-core (#6356)
Browse files Browse the repository at this point in the history
* Remove dummy from cirq-core

- Latest best practice advice is to remove all mentions
of the word 'dummy' as it is problematic terminology.
  • Loading branch information
dstrain115 authored Nov 22, 2023
1 parent 3a64ef1 commit 2fdb447
Show file tree
Hide file tree
Showing 26 changed files with 120 additions and 123 deletions.
16 changes: 8 additions & 8 deletions cirq-core/cirq/circuits/qasm_output_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,26 @@ class UnsupportedOperation(cirq.Operation):


def _all_operations(q0, q1, q2, q3, q4, include_measurements=True):
class DummyOperation(cirq.Operation):
class ExampleOperation(cirq.Operation):
qubits = (q0,)
with_qubits = NotImplemented

def _qasm_(self, args: cirq.QasmArgs) -> str:
return '// Dummy operation\n'
return '// Example operation\n'

def _decompose_(self):
# Only used by test_output_unitary_same_as_qiskit
return () # pragma: no cover

class DummyCompositeOperation(cirq.Operation):
class ExampleCompositeOperation(cirq.Operation):
qubits = (q0,)
with_qubits = NotImplemented

def _decompose_(self):
return cirq.X(self.qubits[0])

def __repr__(self):
return 'DummyCompositeOperation()'
return 'ExampleCompositeOperation()'

return (
cirq.I(q0),
Expand Down Expand Up @@ -328,8 +328,8 @@ def __repr__(self):
)
if include_measurements
else (),
DummyOperation(),
DummyCompositeOperation(),
ExampleOperation(),
ExampleCompositeOperation(),
)


Expand Down Expand Up @@ -539,9 +539,9 @@ def filter_unpredictable_numbers(text):
x q[2]; // Undo the inversion
measure q[3] -> m_multi[2];
// Dummy operation
// Example operation
// Operation: DummyCompositeOperation()
// Operation: ExampleCompositeOperation()
x q[0];
"""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,23 @@ def test_already_converted():


def test_ignore_unsupported_gate():
class UnsupportedDummy(cirq.testing.TwoQubitGate):
class UnsupportedGate(cirq.testing.TwoQubitGate):
pass

q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(UnsupportedDummy()(q0, q1), cirq.X(q0) ** sympy.Symbol("theta"))
c_orig = cirq.Circuit(UnsupportedGate()(q0, q1), cirq.X(q0) ** sympy.Symbol("theta"))
c_new = cirq.optimize_for_target_gateset(
c_orig, gateset=CliffordTargetGateset(), ignore_failures=True
)
assert c_new == c_orig


def test_fail_unsupported_gate():
class UnsupportedDummy(cirq.testing.TwoQubitGate):
class UnsupportedGate(cirq.testing.TwoQubitGate):
pass

q0, q1 = cirq.LineQubit.range(2)
c_orig = cirq.Circuit(UnsupportedDummy()(q0, q1))
c_orig = cirq.Circuit(UnsupportedGate()(q0, q1))
with pytest.raises(ValueError):
_ = cirq.optimize_for_target_gateset(
c_orig, gateset=CliffordTargetGateset(), ignore_failures=False
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/experiments/xeb_fitting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def test_benchmark_2q_xeb_fidelities_parallel():

def test_benchmark_2q_xeb_fidelities_vectorized():
rs = np.random.RandomState(52)
dummy_records = [{'pure_probs': rs.rand(4), 'sampled_probs': rs.rand(4)} for _ in range(100)]
df = pd.DataFrame(dummy_records)
mock_records = [{'pure_probs': rs.rand(4), 'sampled_probs': rs.rand(4)} for _ in range(100)]
df = pd.DataFrame(mock_records)

# Using `df.apply` is wayyyy slower than the new implementation!
# but they should give the same results
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/experiments/xeb_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _verify_two_line_qubits_from_circuits(circuits: Sequence['cirq.Circuit']):


class _NoProgress:
"""Dummy (lack of) tqdm-style progress bar."""
"""Lack of tqdm-style progress bar."""

def __init__(self, total: int):
pass
Expand Down
7 changes: 3 additions & 4 deletions cirq-core/cirq/ops/clifford_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pytest

import cirq
from cirq.protocols.act_on_protocol_test import DummySimulationState
from cirq.protocols.act_on_protocol_test import ExampleSimulationState
from cirq.testing import EqualsTester, assert_allclose_up_to_global_phase

_bools = (False, True)
Expand All @@ -47,7 +47,7 @@ def _assert_no_collision(gate) -> None:


def _all_rotations():
for (pauli, flip) in itertools.product(_paulis, _bools):
for pauli, flip in itertools.product(_paulis, _bools):
yield (pauli, flip)


Expand Down Expand Up @@ -490,7 +490,6 @@ def test_commutes_pauli(gate, pauli, half_turns):


def test_to_clifford_tableau_util_function():

tableau = cirq.ops.clifford_gate._to_clifford_tableau(
x_to=(cirq.X, False), z_to=(cirq.Z, False)
)
Expand Down Expand Up @@ -879,7 +878,7 @@ def test_clifford_gate_act_on_ch_form():

def test_clifford_gate_act_on_fail():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.CliffordGate.X, DummySimulationState(), qubits=())
cirq.act_on(cirq.CliffordGate.X, ExampleSimulationState(), qubits=())


def test_all_single_qubit_clifford_unitaries():
Expand Down
8 changes: 3 additions & 5 deletions cirq-core/cirq/ops/common_channels_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pytest

import cirq
from cirq.protocols.act_on_protocol_test import DummySimulationState
from cirq.protocols.act_on_protocol_test import ExampleSimulationState

X = np.array([[0, 1], [1, 0]])
Y = np.array([[0, -1j], [1j, 0]])
Expand Down Expand Up @@ -87,7 +87,6 @@ def test_asymmetric_depolarizing_channel_str():


def test_asymmetric_depolarizing_channel_eq():

a = cirq.asymmetric_depolarize(0.0099999, 0.01)
b = cirq.asymmetric_depolarize(0.01, 0.0099999)
c = cirq.asymmetric_depolarize(0.0, 0.0, 0.0)
Expand Down Expand Up @@ -492,7 +491,7 @@ def test_reset_channel_text_diagram():

def test_reset_act_on():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.ResetChannel(), DummySimulationState(), qubits=())
cirq.act_on(cirq.ResetChannel(), ExampleSimulationState(), qubits=())

args = cirq.StateVectorSimulationState(
available_buffer=np.empty(shape=(2, 2, 2, 2, 2), dtype=np.complex64),
Expand Down Expand Up @@ -697,7 +696,6 @@ def test_bit_flip_channel_str():


def test_bit_flip_channel_eq():

a = cirq.bit_flip(0.0099999)
b = cirq.bit_flip(0.01)
c = cirq.bit_flip(0.0)
Expand Down Expand Up @@ -735,7 +733,7 @@ def test_bit_flip_channel_text_diagram():
def test_stabilizer_supports_depolarize():
with pytest.raises(TypeError, match="act_on"):
for _ in range(100):
cirq.act_on(cirq.depolarize(3 / 4), DummySimulationState(), qubits=())
cirq.act_on(cirq.depolarize(3 / 4), ExampleSimulationState(), qubits=())

q = cirq.LineQubit(0)
c = cirq.Circuit(cirq.depolarize(3 / 4).on(q), cirq.measure(q, key='m'))
Expand Down
14 changes: 7 additions & 7 deletions cirq-core/cirq/ops/common_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sympy

import cirq
from cirq.protocols.act_on_protocol_test import DummySimulationState
from cirq.protocols.act_on_protocol_test import ExampleSimulationState

H = np.array([[1, 1], [1, -1]]) * np.sqrt(0.5)
HH = cirq.kron(H, H)
Expand Down Expand Up @@ -310,7 +310,7 @@ def test_h_str():

def test_x_act_on_tableau():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.X, DummySimulationState(), qubits=())
cirq.act_on(cirq.X, ExampleSimulationState(), qubits=())
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
flipped_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=23)

Expand Down Expand Up @@ -359,7 +359,7 @@ def _unitary_(self):

def test_y_act_on_tableau():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.Y, DummySimulationState(), qubits=())
cirq.act_on(cirq.Y, ExampleSimulationState(), qubits=())
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
flipped_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=23)

Expand Down Expand Up @@ -397,9 +397,9 @@ def test_y_act_on_tableau():

def test_z_h_act_on_tableau():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.Z, DummySimulationState(), qubits=())
cirq.act_on(cirq.Z, ExampleSimulationState(), qubits=())
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.H, DummySimulationState(), qubits=())
cirq.act_on(cirq.H, ExampleSimulationState(), qubits=())
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)
flipped_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=23)

Expand Down Expand Up @@ -450,7 +450,7 @@ def test_z_h_act_on_tableau():

def test_cx_act_on_tableau():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.CX, DummySimulationState(), qubits=())
cirq.act_on(cirq.CX, ExampleSimulationState(), qubits=())
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)

state = cirq.CliffordTableauSimulationState(
Expand Down Expand Up @@ -494,7 +494,7 @@ def test_cx_act_on_tableau():

def test_cz_act_on_tableau():
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(cirq.CZ, DummySimulationState(), qubits=())
cirq.act_on(cirq.CZ, ExampleSimulationState(), qubits=())
original_tableau = cirq.CliffordTableau(num_qubits=5, initial_state=31)

state = cirq.CliffordTableauSimulationState(
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/gate_features_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_qasm_output_args_format():


def test_multi_qubit_gate_validate():
class Dummy(cirq.Gate):
class Example(cirq.Gate):
def _num_qubits_(self) -> int:
return self._num_qubits

Expand All @@ -62,7 +62,7 @@ def __init__(self, num_qubits):

a, b, c, d = cirq.LineQubit.range(4)

g = Dummy(3)
g = Example(3)

assert g.num_qubits() == 3
g.validate_args([a, b, c])
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/ops/pauli_string_raw_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def map_qubits(self, qubit_map):
def test_on_wrong_number_qubits():
q0, q1, q2 = _make_qubits(3)

class DummyGate(cirq.PauliStringGateOperation):
class ExampleGate(cirq.PauliStringGateOperation):
def map_qubits(self, qubit_map):
ps = self.pauli_string.map_qubits(qubit_map)
return DummyGate(ps)
return ExampleGate(ps)

g = DummyGate(cirq.PauliString({q0: cirq.X, q1: cirq.Y}))
g = ExampleGate(cirq.PauliString({q0: cirq.X, q1: cirq.Y}))

_ = g.with_qubits(q1, q2)
with pytest.raises(ValueError):
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,11 @@ def test_pass_operations_over_cz():


def test_pass_operations_over_no_common_qubits():
class DummyGate(cirq.testing.SingleQubitGate):
class ExampleGate(cirq.testing.SingleQubitGate):
pass

q0, q1 = _make_qubits(2)
op0 = DummyGate()(q1)
op0 = ExampleGate()(q1)
ps_before = cirq.PauliString({q0: cirq.Z})
ps_after = cirq.PauliString({q0: cirq.Z})
_assert_pass_over([op0], ps_before, ps_after)
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/ops/random_gate_channel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ def test_stabilizer_supports_probability():


def test_unsupported_stabilizer_safety():
from cirq.protocols.act_on_protocol_test import DummySimulationState
from cirq.protocols.act_on_protocol_test import ExampleSimulationState

with pytest.raises(TypeError, match="act_on"):
for _ in range(100):
cirq.act_on(cirq.X.with_probability(0.5), DummySimulationState(), qubits=())
cirq.act_on(cirq.X.with_probability(0.5), ExampleSimulationState(), qubits=())
with pytest.raises(TypeError, match="act_on"):
cirq.act_on(cirq.X.with_probability(sympy.Symbol('x')), DummySimulationState(), qubits=())
cirq.act_on(cirq.X.with_probability(sympy.Symbol('x')), ExampleSimulationState(), qubits=())

q = cirq.LineQubit(0)
c = cirq.Circuit((cirq.X(q) ** 0.25).with_probability(0.5), cirq.measure(q, key='m'))
Expand Down
8 changes: 4 additions & 4 deletions cirq-core/cirq/ops/raw_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,9 @@ def qubits(self):
pass

q = cirq.LineQubit(1)
from cirq.protocols.act_on_protocol_test import DummySimulationState
from cirq.protocols.act_on_protocol_test import ExampleSimulationState

args = DummySimulationState()
args = ExampleSimulationState()
cirq.act_on(YesActOn()(q).with_tags("test"), args)
with pytest.raises(TypeError, match="Failed to act"):
cirq.act_on(NoActOn()(q).with_tags("test"), args)
Expand All @@ -798,11 +798,11 @@ def qubits(self):


def test_single_qubit_gate_validates_on_each():
class Dummy(cirq.testing.SingleQubitGate):
class Example(cirq.testing.SingleQubitGate):
def matrix(self):
pass

g = Dummy()
g = Example()
assert g.num_qubits() == 1

test_qubits = [cirq.NamedQubit(str(i)) for i in range(3)]
Expand Down
18 changes: 9 additions & 9 deletions cirq-core/cirq/protocols/act_on_protocol_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import cirq


class DummyQuantumState(cirq.QuantumStateRepresentation):
class ExampleQuantumState(cirq.QuantumStateRepresentation):
def copy(self, deep_copy_buffers=True):
pass

def measure(self, axes, seed=None):
pass


class DummySimulationState(cirq.SimulationState):
class ExampleSimulationState(cirq.SimulationState):
def __init__(self, fallback_result: Any = NotImplemented):
super().__init__(prng=np.random.RandomState(), state=DummyQuantumState())
super().__init__(prng=np.random.RandomState(), state=ExampleQuantumState())
self.fallback_result = fallback_result

def _act_on_fallback_(
Expand All @@ -43,18 +43,18 @@ def _act_on_fallback_(


def test_act_on_fallback_succeeds():
state = DummySimulationState(fallback_result=True)
state = ExampleSimulationState(fallback_result=True)
cirq.act_on(op, state)


def test_act_on_fallback_fails():
state = DummySimulationState(fallback_result=NotImplemented)
state = ExampleSimulationState(fallback_result=NotImplemented)
with pytest.raises(TypeError, match='Failed to act'):
cirq.act_on(op, state)


def test_act_on_fallback_errors():
state = DummySimulationState(fallback_result=False)
state = ExampleSimulationState(fallback_result=False)
with pytest.raises(ValueError, match='_act_on_fallback_ must return True or NotImplemented'):
cirq.act_on(op, state)

Expand All @@ -71,7 +71,7 @@ def with_qubits(self, *new_qubits: 'cirq.Qid') -> Self: # type: ignore[empty-bo
def _act_on_(self, sim_state):
return False

state = DummySimulationState(fallback_result=True)
state = ExampleSimulationState(fallback_result=True)
with pytest.raises(ValueError, match='_act_on_ must return True or NotImplemented'):
cirq.act_on(Op(), state)

Expand All @@ -85,14 +85,14 @@ def qubits(self) -> Tuple['cirq.Qid', ...]: # type: ignore[empty-body]
def with_qubits(self, *new_qubits: 'cirq.Qid') -> Self: # type: ignore[empty-body]
pass

state = DummySimulationState()
state = ExampleSimulationState()
with pytest.raises(
ValueError, match='Calls to act_on should not supply qubits if the action is an Operation'
):
cirq.act_on(Op(), state, qubits=[])


def test_qubits_should_be_defined_for_operations():
state = DummySimulationState()
state = ExampleSimulationState()
with pytest.raises(ValueError, match='Calls to act_on should'):
cirq.act_on(cirq.KrausChannel([np.array([[1, 0], [0, 0]])]), state, qubits=None)
Loading

0 comments on commit 2fdb447

Please sign in to comment.