Skip to content

Commit

Permalink
Remove device from circuits (#5189)
Browse files Browse the repository at this point in the history
Removes v0.15 deprecation items for attaching devices to circuits.
  • Loading branch information
MichaelBroughton authored Apr 11, 2022
1 parent 8eb928d commit 0243555
Show file tree
Hide file tree
Showing 34 changed files with 575 additions and 1,027 deletions.
234 changes: 36 additions & 198 deletions cirq-core/cirq/circuits/circuit.py

Large diffs are not rendered by default.

45 changes: 4 additions & 41 deletions cirq-core/cirq/circuits/circuit_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from typing import Any, Callable, Dict, Generic, Iterator, TypeVar, cast, TYPE_CHECKING

import functools
import networkx

from cirq import _compat, ops, devices
from cirq import ops
from cirq.circuits import circuit

if TYPE_CHECKING:
Expand Down Expand Up @@ -70,17 +69,10 @@ class CircuitDag(networkx.DiGraph):

disjoint_qubits = staticmethod(_disjoint_qubits)

@_compat.deprecated_parameter(
deadline='v0.15',
fix=circuit._DEVICE_DEP_MESSAGE,
parameter_desc='device',
match=lambda args, kwargs: 'device' in kwargs or len(args) == 4,
)
def __init__(
self,
can_reorder: Callable[['cirq.Operation', 'cirq.Operation'], bool] = _disjoint_qubits,
incoming_graph_data: Any = None,
device: 'cirq.Device' = devices.UNCONSTRAINED_DEVICE,
) -> None:
"""Initializes a CircuitDag.
Expand All @@ -98,15 +90,6 @@ def __init__(
"""
super().__init__(incoming_graph_data)
self.can_reorder = can_reorder
self._device = device

@property # type: ignore
@_compat.deprecated(
deadline='v0.15',
fix=circuit._DEVICE_DEP_MESSAGE,
)
def device(self) -> devices.Device:
return self._device

@staticmethod
def make_node(op: 'cirq.Operation') -> Unique:
Expand All @@ -117,30 +100,14 @@ def from_circuit(
circuit: circuit.Circuit,
can_reorder: Callable[['cirq.Operation', 'cirq.Operation'], bool] = _disjoint_qubits,
) -> 'CircuitDag':
if circuit._device == devices.UNCONSTRAINED_DEVICE:
return CircuitDag.from_ops(circuit.all_operations(), can_reorder=can_reorder)
return CircuitDag.from_ops(
circuit.all_operations(), can_reorder=can_reorder, device=circuit._device
)
return CircuitDag.from_ops(circuit.all_operations(), can_reorder=can_reorder)

@staticmethod
@_compat.deprecated_parameter(
deadline='v0.15',
fix=circuit._DEVICE_DEP_MESSAGE,
parameter_desc='device',
match=lambda args, kwargs: 'device' in kwargs,
)
def from_ops(
*operations: 'cirq.OP_TREE',
can_reorder: Callable[['cirq.Operation', 'cirq.Operation'], bool] = _disjoint_qubits,
device: 'cirq.Device' = devices.UNCONSTRAINED_DEVICE,
) -> 'CircuitDag':
if device == devices.UNCONSTRAINED_DEVICE:
dag = CircuitDag(can_reorder=can_reorder)
else:
with _compat.block_overlapping_deprecation(re.escape(circuit._DEVICE_DEP_MESSAGE)):
dag = CircuitDag(can_reorder=can_reorder, device=device)

dag = CircuitDag(can_reorder=can_reorder)
for op in ops.flatten_op_tree(operations):
dag.append(cast(ops.Operation, op))
return dag
Expand Down Expand Up @@ -212,11 +179,7 @@ def all_qubits(self):
return frozenset(q for node in self.nodes for q in node.val.qubits)

def to_circuit(self) -> circuit.Circuit:
if self._device == devices.UNCONSTRAINED_DEVICE:
return circuit.Circuit(self.all_operations(), strategy=circuit.InsertStrategy.EARLIEST)
return circuit.Circuit(
self.all_operations(), strategy=circuit.InsertStrategy.EARLIEST, device=self._device
)
return circuit.Circuit(self.all_operations(), strategy=circuit.InsertStrategy.EARLIEST)

def findall_nodes_until_blocked(
self, is_blocker: Callable[['cirq.Operation'], bool]
Expand Down
45 changes: 0 additions & 45 deletions cirq-core/cirq/circuits/circuit_dag_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@ def test_init():
assert list(dag.edges()) == []


def test_init_device_deprecated():
with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
_ = cirq.CircuitDag(device=cirq.UNCONSTRAINED_DEVICE)


def test_device_deprecated():
dag = cirq.CircuitDag()
with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
_ = dag.device


def test_append():
q0 = cirq.LineQubit(0)
dag = cirq.CircuitDag()
Expand Down Expand Up @@ -118,14 +103,6 @@ def test_from_ops():
assert [(n1.val, n2.val) for n1, n2 in dag.edges()] == [(cirq.X(q0), cirq.Y(q0))]


def test_from_ops_device_deprecated():
with cirq.testing.assert_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=FakeDevice())


def test_from_circuit():
q0 = cirq.LineQubit(0)
circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q0))
Expand All @@ -136,16 +113,6 @@ def test_from_circuit():
assert sorted(circuit.all_qubits()) == sorted(dag.all_qubits())


def test_from_circuit_deprecated():
q0 = cirq.LineQubit(0)
circuit = cirq.Circuit(cirq.X(q0), cirq.Y(q0))
circuit._device = FakeDevice()
with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
_ = cirq.CircuitDag.from_circuit(circuit)


def test_to_empty_circuit():
circuit = cirq.Circuit()
dag = cirq.CircuitDag.from_circuit(circuit)
Expand All @@ -167,18 +134,6 @@ def test_to_circuit():
)


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 = FakeDevice()

with cirq.testing.assert_deprecated(
cirq.circuits.circuit._DEVICE_DEP_MESSAGE, deadline='v0.15'
):
_ = dag.to_circuit()


def test_equality():
q0, q1 = cirq.LineQubit.range(2)
circuit1 = cirq.Circuit(
Expand Down
Loading

0 comments on commit 0243555

Please sign in to comment.