Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate cirq.SingleQubitGate #5272

Merged
merged 14 commits into from
Apr 21, 2022
16 changes: 8 additions & 8 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1884,15 +1884,15 @@ def test_qid_shape_qubit(circuit_cls):

@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_qid_shape_qudit(circuit_cls):
class PlusOneMod3Gate(cirq.SingleQubitGate):
class PlusOneMod3Gate(cirq.testing.SingleQubitGate):
def _qid_shape_(self):
return (3,)

class C2NotGate(cirq.Gate):
def _qid_shape_(self):
return (3, 2)

class IdentityGate(cirq.SingleQubitGate):
class IdentityGate(cirq.testing.SingleQubitGate):
def _qid_shape_(self):
return (1,)

Expand Down Expand Up @@ -1987,13 +1987,13 @@ def test_to_text_diagram_teleportation_to_diagram(circuit_cls):

@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_diagram_with_unknown_exponent(circuit_cls):
class WeirdGate(cirq.SingleQubitGate):
class WeirdGate(cirq.testing.SingleQubitGate):
def _circuit_diagram_info_(
self, args: cirq.CircuitDiagramInfoArgs
) -> cirq.CircuitDiagramInfo:
return cirq.CircuitDiagramInfo(wire_symbols=('B',), exponent='fancy')

class WeirderGate(cirq.SingleQubitGate):
class WeirderGate(cirq.testing.SingleQubitGate):
def _circuit_diagram_info_(
self, args: cirq.CircuitDiagramInfoArgs
) -> cirq.CircuitDiagramInfo:
Expand Down Expand Up @@ -2103,7 +2103,7 @@ def _circuit_diagram_info_(self, args: cirq.CircuitDiagramInfoArgs) -> Tuple[str
def test_to_text_diagram_parameterized_value(circuit_cls):
q = cirq.NamedQubit('cube')

class PGate(cirq.SingleQubitGate):
class PGate(cirq.testing.SingleQubitGate):
def __init__(self, val):
self.val = val

Expand Down Expand Up @@ -2266,10 +2266,10 @@ def test_diagram_global_phase(circuit_cls):

@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_has_unitary(circuit_cls):
class NonUnitary(cirq.SingleQubitGate):
class NonUnitary(cirq.testing.SingleQubitGate):
pass

class EventualUnitary(cirq.SingleQubitGate):
class EventualUnitary(cirq.testing.SingleQubitGate):
def _decompose_(self, qubits):
return cirq.X.on_each(*qubits)

Expand Down Expand Up @@ -4098,7 +4098,7 @@ def test_indexing_by_numpy_integer(circuit_cls):

@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_all_measurement_key_names(circuit_cls):
class Unknown(cirq.SingleQubitGate):
class Unknown(cirq.testing.SingleQubitGate):
def _measurement_key_name_(self):
return 'test'

Expand Down
10 changes: 3 additions & 7 deletions cirq-core/cirq/circuits/moment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,7 @@ def test_moment_text_diagram():
xy_breakdown_func=lambda q: ('abc'[q.x], q.x),
)

class EmptyGate(cirq.Gate):
def _num_qubits_(self) -> int:
return 1

class EmptyGate(cirq.testing.SingleQubitGate):
def __str__(self):
return 'Empty'

Expand Down Expand Up @@ -701,9 +698,8 @@ def test_kraus_too_big():


def test_op_has_no_kraus():
class EmptyGate(cirq.Gate):
def _num_qubits_(self) -> int:
return 1
class EmptyGate(cirq.testing.SingleQubitGate):
pass

m = cirq.Moment(EmptyGate().on(cirq.NamedQubit("a")))
assert not cirq.has_kraus(m)
Expand Down
5 changes: 4 additions & 1 deletion cirq-core/cirq/circuits/qasm_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@value.value_equality(approximate=True)
class QasmUGate(ops.SingleQubitGate):
class QasmUGate(ops.Gate):
def __init__(self, theta, phi, lmda) -> None:
"""A QASM gate representing any single qubit unitary with a series of
three rotations, Z, Y, and Z.
Expand All @@ -42,6 +42,9 @@ def __init__(self, theta, phi, lmda) -> None:
self.theta = theta % 2
self.phi = phi % 2

def _num_qubits_(self) -> int:
return 1

@staticmethod
def from_matrix(mat: np.ndarray) -> 'QasmUGate':
pre_phase, rotation, post_phase = linalg.deconstruct_single_qubit_matrix_into_angles(mat)
Expand Down
5 changes: 4 additions & 1 deletion cirq-core/cirq/circuits/quil_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def to_quil_complex_format(num) -> str:


@value.value_equality(approximate=True)
class QuilOneQubitGate(ops.SingleQubitGate):
class QuilOneQubitGate(ops.Gate):
"""A QUIL gate representing any single qubit unitary with a DEFGATE and
2x2 matrix in QUIL.
"""
Expand All @@ -38,6 +38,9 @@ def __init__(self, matrix: np.ndarray) -> None:
"""
self.matrix = matrix

def _num_qubits_(self) -> int:
return 1

def _quil_(self, qubits: Tuple['cirq.Qid', ...], formatter: 'cirq.QuilFormatter') -> str:
return (
f'DEFGATE USERGATE:\n '
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/qasm_import/_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def test_unknown_function():


@pytest.mark.parametrize('qasm_gate,cirq_gate', rotation_gates)
def test_rotation_gates(qasm_gate: str, cirq_gate: cirq.SingleQubitGate):
def test_rotation_gates(qasm_gate: str, cirq_gate: cirq.Gate):
qasm = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
Expand Down Expand Up @@ -989,7 +989,7 @@ def test_three_qubit_gates_with_too_much_parameters(qasm_gate: str):


@pytest.mark.parametrize('qasm_gate,cirq_gate', single_qubit_gates)
def test_single_qubit_gates(qasm_gate: str, cirq_gate: cirq.SingleQubitGate):
def test_single_qubit_gates(qasm_gate: str, cirq_gate: cirq.Gate):
qasm = """OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/contrib/quirk/export_to_quirk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def with_qubits(self, *new_qubits):
return MysteryOperation(*new_qubits)


class MysteryGate(cirq.SingleQubitGate):
class MysteryGate(cirq.testing.SingleQubitGate):
pass


Expand Down Expand Up @@ -279,7 +279,7 @@ def test_unrecognized_single_qubit_gate_with_matrix():


def test_unknown_gate():
class UnknownGate(cirq.SingleQubitGate):
class UnknownGate(cirq.testing.SingleQubitGate):
pass

a = cirq.NamedQubit('a')
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/devices/noise_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def noisy_moment(
""",
)

NOISE_MODEL_LIKE = Union[None, 'cirq.NoiseModel', 'cirq.SingleQubitGate']
NOISE_MODEL_LIKE = Union[None, 'cirq.NoiseModel', 'cirq.Gate']
document(
NOISE_MODEL_LIKE, # type: ignore
"""A `cirq.NoiseModel` or a value that can be trivially converted into one.
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/experiments/cross_entropy_benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def cross_entropy_benchmarking(
num_circuits: int = 20,
repetitions: int = 1000,
cycles: Union[int, Iterable[int]] = range(2, 103, 10),
scrambling_gates_per_cycle: List[List[ops.SingleQubitGate]] = None,
scrambling_gates_per_cycle: List[List[ops.Gate]] = None,
simulator: sim.Simulator = None,
) -> CrossEntropyResult:
r"""Cross-entropy benchmarking (XEB) of multiple qubits.
Expand Down Expand Up @@ -482,7 +482,7 @@ def build_entangling_layers(
def _build_xeb_circuits(
qubits: Sequence[ops.Qid],
cycles: Sequence[int],
single_qubit_gates: List[List[ops.SingleQubitGate]] = None,
single_qubit_gates: List[List[ops.Gate]] = None,
benchmark_ops: Sequence[circuits.Moment] = None,
) -> List[circuits.Circuit]:
if benchmark_ops is not None:
Expand Down Expand Up @@ -570,7 +570,7 @@ def _random_half_rotations(qubits: Sequence[ops.Qid], num_layers: int) -> List[L


def _random_any_gates(
qubits: Sequence[ops.Qid], op_list: List[List[ops.SingleQubitGate]], num_layers: int
qubits: Sequence[ops.Qid], op_list: List[List[ops.Gate]], num_layers: int
) -> List[List[ops.OP_TREE]]:
num_ops = len(op_list)
num_qubits = len(qubits)
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/interop/quirk/cells/control_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def generate_all_control_cell_makers() -> Iterator[CellMaker]:
yield _reg_parity_control("zpar", basis_change=None)


def _reg_control(identifier: str, *, basis_change: Optional['cirq.SingleQubitGate']) -> CellMaker:
def _reg_control(identifier: str, *, basis_change: Optional['cirq.Gate']) -> CellMaker:
return CellMaker(
identifier=identifier,
size=1,
Expand All @@ -143,7 +143,7 @@ def _reg_control(identifier: str, *, basis_change: Optional['cirq.SingleQubitGat


def _reg_parity_control(
identifier: str, *, basis_change: Optional['cirq.SingleQubitGate'] = None
identifier: str, *, basis_change: Optional['cirq.Gate'] = None
) -> CellMaker:
return CellMaker(
identifier=identifier,
Expand All @@ -155,7 +155,7 @@ def _reg_parity_control(


def _basis_else_empty(
basis_change: Optional['cirq.SingleQubitGate'], qureg: Union['cirq.Qid', Iterable['cirq.Qid']]
basis_change: Optional['cirq.Gate'], qureg: Union['cirq.Qid', Iterable['cirq.Qid']]
) -> Iterable['cirq.Operation']:
if basis_change is None:
return ()
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ion/convert_to_ion_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
import cirq


class OtherX(cirq.SingleQubitGate):
class OtherX(cirq.testing.SingleQubitGate):
def _unitary_(self) -> np.ndarray:
return np.array([[0, 1], [1, 0]])


class NoUnitary(cirq.SingleQubitGate):
class NoUnitary(cirq.testing.SingleQubitGate):
pass


Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/ion/ion_device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_init():
assert d.duration_of(cirq.measure(q0)) == 100 * ms
assert d.duration_of(cirq.measure(q0, q1)) == 100 * ms
assert d.duration_of(cirq.ops.XX(q0, q1)) == 200 * ms
with pytest.raises(ValueError):
_ = d.duration_of(cirq.SingleQubitGate().on(q0))
with pytest.raises(ValueError, match="Unsupported gate type"):
_ = d.duration_of(cirq.I(q0))

with pytest.raises(TypeError, match="NamedQubit"):
_ = cirq.IonDevice(
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_init_timedelta():
assert d.duration_of(cirq.measure(q0, q1)) == 100 * ms
assert d.duration_of(cirq.ops.XX(q0, q1)) == 200 * ms
with pytest.raises(ValueError):
_ = d.duration_of(cirq.SingleQubitGate().on(q0))
_ = d.duration_of(cirq.testing.SingleQubitGate().on(q0))


def test_decomposition_deprecated():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def with_qubits(self, *new_qubits):


def test_avoids_decompose_fallback_when_matrix_available_single_qubit():
class OtherX(cirq.SingleQubitGate):
class OtherX(cirq.testing.SingleQubitGate):
def _unitary_(self) -> np.ndarray:
return np.array([[0, 1], [1, 0]])

class OtherOtherX(cirq.SingleQubitGate):
class OtherOtherX(cirq.testing.SingleQubitGate):
def _decompose_(self, qubits):
return OtherX().on(*qubits)

Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/neutral_atoms/neutral_atom_devices_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_init():
assert d.duration_of(cirq.GateOperation(cirq.IdentityGate(1), [q00])) == 100 * us
assert d.duration_of(cirq.measure(q00)) == 50 * ms
with pytest.raises(ValueError):
_ = d.duration_of(cirq.SingleQubitGate().on(q00))
_ = d.duration_of(cirq.testing.SingleQubitGate().on(q00))


def test_metadata():
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_init_timedelta():
assert d.duration_of(cirq.GateOperation(cirq.IdentityGate(1), [q00])) == 100 * us
assert d.duration_of(cirq.measure(q00)) == 50 * ms
with pytest.raises(ValueError):
_ = d.duration_of(cirq.SingleQubitGate().on(q00))
_ = d.duration_of(cirq.testing.SingleQubitGate().on(q00))


def test_init_errors():
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_validate_gate_errors():
with pytest.raises(ValueError, match="controlled gates must have integer exponents"):
d.validate_gate(cirq.CNotPowGate(exponent=0.5))
with pytest.raises(ValueError, match="Unsupported gate"):
d.validate_gate(cirq.SingleQubitGate())
d.validate_gate(cirq.testing.SingleQubitGate())


def test_validate_operation_errors():
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _validate_map_input(
def _pad_tableau(
clifford_tableau: qis.CliffordTableau, num_qubits_after_padding: int, axes: List[int]
) -> qis.CliffordTableau:
"""Roughly, this function copies self.tabluea into the "identity" matrix."""
"""Roughly, this function copies self.tableau into the "identity" matrix."""
# Sanity check
if len(set(axes)) != clifford_tableau.n:
raise ValueError(
Expand Down
Loading