Skip to content

Commit

Permalink
Remove deprecated SingleQubitGate (#5686)
Browse files Browse the repository at this point in the history
Note: cirq.testing.SingleQubitGate still exists for brevity when writing tests.)
  • Loading branch information
dstrain115 committed Jul 8, 2022
1 parent 90d4adc commit b47cee8
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 111 deletions.
1 change: 0 additions & 1 deletion cirq-core/cirq/__init__.py
Expand Up @@ -301,7 +301,6 @@
rz,
S,
SingleQubitCliffordGate,
SingleQubitGate,
SingleQubitPauliStringGateOperation,
SQRT_ISWAP,
SQRT_ISWAP_INV,
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/ops/__init__.py
Expand Up @@ -87,7 +87,7 @@

from cirq.ops.fsim_gate import FSimGate, PhasedFSimGate

from cirq.ops.gate_features import InterchangeableQubitsGate, SingleQubitGate
from cirq.ops.gate_features import InterchangeableQubitsGate

from cirq.ops.gate_operation import GateOperation

Expand Down
23 changes: 0 additions & 23 deletions cirq-core/cirq/ops/gate_features.py
Expand Up @@ -18,11 +18,6 @@
"""

import abc
import warnings

from cirq import value
from cirq._compat import deprecated_class
from cirq.ops import raw_types


class InterchangeableQubitsGate(metaclass=abc.ABCMeta):
Expand All @@ -31,21 +26,3 @@ class InterchangeableQubitsGate(metaclass=abc.ABCMeta):
def qubit_index_to_equivalence_group_key(self, index: int) -> int:
"""Returns a key that differs between non-interchangeable qubits."""
return 0


class _SingleQubitGateMeta(value.ABCMetaImplementAnyOneOf):
def __instancecheck__(cls, instance):
warnings.warn(
'isinstance(gate, SingleQubitGate) is deprecated. '
'Use cirq.num_qubits(gate) == 1 instead',
DeprecationWarning,
)
return isinstance(instance, raw_types.Gate) and instance._num_qubits_() == 1


@deprecated_class(deadline='v1.0', fix='Define _num_qubits_ manually.')
class SingleQubitGate(raw_types.Gate, metaclass=_SingleQubitGateMeta):
"""A gate that must be applied to exactly one qubit."""

def _num_qubits_(self) -> int:
return 1
65 changes: 0 additions & 65 deletions cirq-core/cirq/ops/gate_features_test.py
Expand Up @@ -15,71 +15,6 @@
import pytest

import cirq
from cirq.testing import assert_deprecated


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_single_qubit_gate_validates_on_each():
class Dummy(cirq.Gate):
def matrix(self):
pass

def _num_qubits_(self) -> int:
return 1

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

test_qubits = [cirq.NamedQubit(str(i)) for i in range(3)]

_ = g.on_each(*test_qubits)
_ = g.on_each(test_qubits)

test_non_qubits = [str(i) for i in range(3)]
with pytest.raises(ValueError):
_ = g.on_each(*test_non_qubits)
with pytest.raises(ValueError):
_ = g.on_each(*test_non_qubits)


def test_single_qubit_validates_on():
class Dummy(cirq.Gate):
def matrix(self):
pass

def _num_qubits_(self) -> int:
return 1

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

test_qubits = [cirq.NamedQubit(str(i)) for i in range(3)]

with pytest.raises(ValueError):
_ = g.on(*test_qubits)
with pytest.raises(ValueError):
_ = g.on(*test_qubits)


def test_qasm_output_args_validate():
Expand Down
2 changes: 0 additions & 2 deletions cirq-core/cirq/ops/gate_operation_test.py
Expand Up @@ -499,8 +499,6 @@ def all_subclasses(cls):
cirq.circuits.quil_output.QuilTwoQubitGate,
cirq.circuits.quil_output.QuilOneQubitGate,
cirq.ops.MSGate,
# Gate features.
cirq.SingleQubitGate,
# Interop gates
cirq.interop.quirk.QuirkQubitPermutationGate,
cirq.interop.quirk.QuirkArithmeticGate,
Expand Down
3 changes: 1 addition & 2 deletions cirq-core/cirq/ops/raw_types.py
Expand Up @@ -192,8 +192,7 @@ class Gate(metaclass=value.ABCMetaImplementAnyOneOf):
Gates operate on a certain number of qubits. All implementations of gate
must implement the `num_qubits` method declaring how many qubits they
act on. The gate feature classes `SingleQubitGate` and `TwoQubitGate`
can be used to avoid writing this boilerplate.
act on.
Linear combinations of gates can be created by adding gates together and
multiplying them by scalars.
Expand Down
1 change: 0 additions & 1 deletion cirq-core/cirq/protocols/json_test_data/spec.py
Expand Up @@ -117,7 +117,6 @@
'Device',
'InterchangeableQubitsGate',
'Pauli',
'SingleQubitGate',
'ABCMetaImplementAnyOneOf',
'SimulatesAmplitudes',
'SimulatesExpectationValues',
Expand Down
Expand Up @@ -15,7 +15,7 @@
"""Utility methods related to optimizing quantum circuits."""

import math
from typing import List, Optional, Tuple, cast
from typing import List, Optional, Tuple

import numpy as np
import sympy
Expand Down Expand Up @@ -111,7 +111,7 @@ def single_qubit_matrix_to_gates(mat: np.ndarray, tolerance: float = 0) -> List[
operation.
"""
rotations = single_qubit_matrix_to_pauli_rotations(mat, tolerance)
return [cast(ops.SingleQubitGate, pauli) ** ht for pauli, ht in rotations]
return [pauli**ht for pauli, ht in rotations]


def single_qubit_op_to_framed_phase_form(mat: np.ndarray) -> Tuple[np.ndarray, complex, complex]:
Expand Down
9 changes: 0 additions & 9 deletions docs/build/custom_gates.ipynb
Expand Up @@ -222,15 +222,6 @@
"print(res)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "874bdef876ed"
},
"source": [
"An alternative to inheriting from `cirq.Gate` is to inherit from `cirq.SingleQubitGate`, in which case defining `_num_qubits_` is unnecessary. An example of a defining a two-qubit gate is shown below."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
4 changes: 3 additions & 1 deletion docs/simulate/noisy_simulation.ipynb
Expand Up @@ -476,7 +476,9 @@
"outputs": [],
"source": [
"\"\"\"Minimal example of defining a custom channel.\"\"\"\n",
"class BitAndPhaseFlipChannel(cirq.SingleQubitGate):\n",
"class BitAndPhaseFlipChannel(cirq.Gate):\n",
" def _num_qubits_(self) -> int:\n",
" return 1\n",
" def __init__(self, p: float) -> None:\n",
" self._p = p\n",
" \n",
Expand Down
14 changes: 10 additions & 4 deletions docs/start/intro.ipynb
Expand Up @@ -1525,7 +1525,7 @@
"\\end{array} \\right]\n",
"$$\n",
"\n",
"Below is a simple implementation of this gate in Cirq. To do this we simply define a class that inherits from `cirq.SingleQubitGate` and implements the `cirq.SupportsUnitary` protocol by implementing the `_unitary_(self)` method. We also define an optional `__str__` representation which Cirq will use when printing this gate out in a circuit diagram."
"Below is a simple implementation of this gate in Cirq. To do this we simply define a class that inherits from `cirq.Gate` and implements the `cirq.SupportsUnitary` protocol by implementing the `_unitary_(self)` method. We also define an optional `__str__` representation which Cirq will use when printing this gate out in a circuit diagram."
]
},
{
Expand All @@ -1539,7 +1539,9 @@
"\"\"\"Example of defining a custom gate in Cirq.\"\"\"\n",
"\n",
"\n",
"class RationalGate(cirq.SingleQubitGate):\n",
"class RationalGate(cirq.Gate):\n",
" def _num_qubits_(self) -> int:\n",
" return 1\n",
" def _unitary_(self):\n",
" return np.array([[3 / 5, 4 / 5], [-4 / 5, 3 / 5]])\n",
"\n",
Expand Down Expand Up @@ -1780,7 +1782,9 @@
"\"\"\"Example of a custom gate which supports the decompose protocol.\"\"\"\n",
"\n",
"\n",
"class HXGate(cirq.SingleQubitGate):\n",
"class HXGate(cirq.Gate):\n",
" def _num_qubits_(self) -> int:\n",
" return 1\n",
" def _decompose_(self, qubits):\n",
" return cirq.H(*qubits), cirq.X(*qubits)\n",
"\n",
Expand Down Expand Up @@ -2832,7 +2836,9 @@
},
"outputs": [],
"source": [
"class InconsistentXGate(cirq.SingleQubitGate):\n",
"class InconsistentXGate(cirq.Gate):\n",
" def _num_qubits_(self) -> int:\n",
" return 1\n",
" def _decompose_(self, qubits):\n",
" yield cirq.H(qubits[0])\n",
" yield cirq.Z(qubits[0])\n",
Expand Down

0 comments on commit b47cee8

Please sign in to comment.