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

Creates fake grid device for testing qubit connectivity in routing #5830

6 changes: 6 additions & 0 deletions cirq-core/cirq/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,10 @@
FakePrinter,
)

from cirq.testing.routing_devices import (
construct_square_device,
construct_ring_device,
RoutingTestingDevice,
)

from cirq.testing.sample_circuits import nonoptimal_toffoli_circuit
75 changes: 75 additions & 0 deletions cirq-core/cirq/testing/routing_devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2021 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
"""Provides test devices that can validate circuits during a routing procedure."""

from typing import Optional, TYPE_CHECKING
import networkx as nx

from cirq import devices

if TYPE_CHECKING:
import cirq


class RoutingTestingDevice(devices.Device):
"""Testing device to be used only for testing qubit connectivity in routing procedures."""
ammareltigani marked this conversation as resolved.
Show resolved Hide resolved
ammareltigani marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, metadata: devices.DeviceMetadata) -> None:
self._metadata = metadata

@property
def metadata(self) -> Optional[devices.DeviceMetadata]:
ammareltigani marked this conversation as resolved.
Show resolved Hide resolved
return self._metadata

def validate_operation(self, operation: 'cirq.Operation') -> None:
for q in operation.qubits:
if q not in self._metadata.qubit_set:
ammareltigani marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(f'Qubit not on device: {q!r}.')

if len(operation.qubits) == 2 and operation.qubits not in self._metadata.nx_graph.edges:
ammareltigani marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(f'Qubit pair is not valid on device: {operation.qubits!r}.')


def construct_square_device(d: int) -> RoutingTestingDevice:
qubits = devices.GridQubit.square(d)

nx_graph = nx.Graph()
row_edges = [
(devices.GridQubit(i, j), devices.GridQubit(i, j + 1))
for i in range(d)
for j in range(d - 1)
]
col_edges = [
(devices.GridQubit(i, j), devices.GridQubit(i + 1, j))
for j in range(d)
for i in range(d - 1)
]
nx_graph.add_edges_from(row_edges)
nx_graph.add_edges_from(col_edges)
ammareltigani marked this conversation as resolved.
Show resolved Hide resolved

metadata = devices.DeviceMetadata(qubits, nx_graph)
return RoutingTestingDevice(metadata)


def construct_ring_device(d: int, directed: bool = False) -> RoutingTestingDevice:
qubits = devices.LineQubit.range(d)
if directed:
nx_graph = nx.DiGraph()
else:
nx_graph = nx.Graph()
edges = [(qubits[i % d], qubits[(i + 1) % d]) for i in range(d)]
nx_graph.add_edges_from(edges)

metadata = devices.DeviceMetadata(qubits, nx_graph)
return RoutingTestingDevice(metadata)
70 changes: 70 additions & 0 deletions cirq-core/cirq/testing/routing_devices_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2021 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 pytest
import networkx as nx
import cirq


def test_square_device():
device = cirq.testing.construct_square_device(5)
device_graph = device.metadata.nx_graph

assert all(q in device_graph.nodes for q in cirq.GridQubit.square(5))
assert nx.is_isomorphic(nx.grid_2d_graph(5, 5), device_graph)


def test_square_op_validation():
device = cirq.testing.construct_square_device(5)

with pytest.raises(ValueError, match="Qubit not on device"):
device.validate_operation(cirq.X(cirq.NamedQubit("a")))
with pytest.raises(ValueError, match="Qubit not on device"):
device.validate_operation(cirq.CNOT(cirq.NamedQubit("a"), cirq.GridQubit(0, 0)))
with pytest.raises(ValueError, match="Qubit not on device"):
device.validate_operation(cirq.CNOT(cirq.GridQubit(5, 4), cirq.GridQubit(4, 4)))

with pytest.raises(ValueError, match="Qubit pair is not valid on device"):
device.validate_operation(cirq.CNOT(cirq.GridQubit(0, 0), cirq.GridQubit(0, 2)))
with pytest.raises(ValueError, match="Qubit pair is not valid on device"):
device.validate_operation(cirq.CNOT(cirq.GridQubit(2, 0), cirq.GridQubit(0, 0)))

device.validate_operation(cirq.CNOT(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)))
device.validate_operation(cirq.CNOT(cirq.GridQubit(1, 0), cirq.GridQubit(0, 0)))


def test_ring_device():
device = cirq.testing.construct_ring_device(5)
device_graph = device.metadata.nx_graph

assert all(q in device_graph.nodes for q in cirq.LineQubit.range(5))
assert nx.is_isomorphic(nx.cycle_graph(5), device_graph)


def test_ring_op_validation():
directed_device = cirq.testing.construct_ring_device(5, directed=True)
undirected_device = cirq.testing.construct_ring_device(5, directed=False)

with pytest.raises(ValueError, match="Qubit not on device"):
directed_device.validate_operation(cirq.X(cirq.LineQubit(5)))
with pytest.raises(ValueError, match="Qubit not on device"):
undirected_device.validate_operation(cirq.X(cirq.LineQubit(5)))

with pytest.raises(ValueError, match="Qubit pair is not valid on device"):
undirected_device.validate_operation(cirq.CNOT(cirq.LineQubit(0), cirq.LineQubit(2)))
with pytest.raises(ValueError, match="Qubit pair is not valid on device"):
directed_device.validate_operation(cirq.CNOT(cirq.LineQubit(1), cirq.LineQubit(0)))

undirected_device.validate_operation(cirq.CNOT(cirq.LineQubit(0), cirq.LineQubit(1)))
undirected_device.validate_operation(cirq.CNOT(cirq.LineQubit(1), cirq.LineQubit(0)))
directed_device.validate_operation(cirq.CNOT(cirq.LineQubit(0), cirq.LineQubit(1)))