Skip to content

Commit

Permalink
Remove cirq.google reference from contrib.quantum_volume and contrib.…
Browse files Browse the repository at this point in the history
…routing (#3888)

Deprecates `cirq.contrib.routing.xmon_device_to_graph` and removes references to cirq.google in `quantum_volume` and `routing` packages.

Related to #3737.

cc @mpharrigan
  • Loading branch information
balopat committed Mar 11, 2021
1 parent 05a499c commit 745790e
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 46 deletions.
36 changes: 25 additions & 11 deletions cirq/contrib/quantum_volume/quantum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
https://arxiv.org/abs/1811.12926.
"""

from typing import Optional, List, cast, Callable, Dict, Tuple, Set, Union
from typing import Optional, List, cast, Callable, Dict, Tuple, Set, Any
from dataclasses import dataclass

import numpy as np
Expand All @@ -11,6 +11,7 @@

import cirq
import cirq.contrib.routing as ccr
from cirq._compat import deprecated_parameter


def generate_model_circuit(
Expand Down Expand Up @@ -422,12 +423,32 @@ def execute_circuits(
return results


def _get_device_graph(device_or_qubits: Any):
qubits = device_or_qubits if isinstance(device_or_qubits, list) else device_or_qubits.qubits
return ccr.gridqubits_to_graph_device(qubits)


@deprecated_parameter(
deadline="v0.12",
fix="use device_graph instead",
parameter_desc='device_or_qubits',
match=lambda args, kwargs: 'device_or_qubits' in kwargs,
rewrite=lambda args, kwargs: (
args,
dict(
('device_graph', _get_device_graph(arg_val))
if arg_name == 'device_or_qubits'
else (arg_name, arg_val)
for arg_name, arg_val in kwargs.items()
),
),
)
def calculate_quantum_volume(
*,
num_qubits: int,
depth: int,
num_circuits: int,
device_or_qubits: Union[cirq.google.XmonDevice, List[cirq.GridQubit]],
device_graph: nx.Graph,
samplers: List[cirq.Sampler],
random_state: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
compiler: Callable[[cirq.Circuit], cirq.Circuit] = None,
Expand All @@ -449,8 +470,8 @@ def calculate_quantum_volume(
depth: The number of gate layers to generate.
num_circuits: The number of random circuits to run.
random_state: Random state or random state seed.
device_or_qubits: The device or the device qubits to run the compiled
circuit on.
device_graph: A graph whose nodes are qubits and edges represent two qubit interactions to
run the compiled circuit on.
samplers: The samplers to run the algorithm on.
compiler: An optional function to compiler the model circuit's
gates down to the target devices gate set and the optimize it.
Expand All @@ -473,13 +494,6 @@ def calculate_quantum_volume(
num_qubits=num_qubits, depth=depth, num_circuits=num_circuits, random_state=random_state
)

# Get the device graph from the given qubits or device.
device_graph = None
if isinstance(device_or_qubits, list):
device_graph = ccr.gridqubits_to_graph_device(device_or_qubits)
else:
device_graph = ccr.xmon_device_to_graph(device_or_qubits)

return execute_circuits(
circuits=circuits,
device_graph=device_graph,
Expand Down
65 changes: 54 additions & 11 deletions cirq/contrib/quantum_volume/quantum_volume_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from cirq.contrib.quantum_volume import CompilationResult


class TestDevice(cirq.Device):
qubits = cirq.GridQubit.rect(5, 5)


def test_generate_model_circuit():
"""Test that a model circuit is randomly generated."""
model_circuit = cirq.contrib.quantum_volume.generate_model_circuit(
Expand Down Expand Up @@ -118,7 +122,7 @@ def test_compile_circuit_router():
router_mock = MagicMock()
cirq.contrib.quantum_volume.compile_circuit(
cirq.Circuit(),
device_graph=ccr.xmon_device_to_graph(cirq.google.Bristlecone),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
router=router_mock,
routing_attempts=1,
)
Expand All @@ -136,15 +140,15 @@ def test_compile_circuit():
)
compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.xmon_device_to_graph(cirq.google.Bristlecone),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
compiler=compiler_mock,
routing_attempts=1,
)

assert len(compilation_result.mapping) == 3
assert cirq.contrib.routing.ops_are_consistent_with_device_graph(
compilation_result.circuit.all_operations(),
cirq.contrib.routing.xmon_device_to_graph(cirq.google.Bristlecone),
cirq.contrib.routing.gridqubits_to_graph_device(TestDevice().qubits),
)
compiler_mock.assert_called_with(compilation_result.circuit)

Expand All @@ -164,7 +168,7 @@ def test_compile_circuit_replaces_swaps():
)
compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.xmon_device_to_graph(cirq.google.Bristlecone),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
compiler=compiler_mock,
routing_attempts=1,
)
Expand Down Expand Up @@ -204,7 +208,7 @@ def test_compile_circuit_with_readout_correction():
)
compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.xmon_device_to_graph(cirq.google.Bristlecone),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
compiler=compiler_mock,
router=router_mock,
routing_attempts=1,
Expand Down Expand Up @@ -253,7 +257,7 @@ def test_compile_circuit_multiple_routing_attempts():

compilation_result = cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.xmon_device_to_graph(cirq.google.Bristlecone),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
compiler=compiler_mock,
router=router_mock,
routing_attempts=3,
Expand All @@ -276,7 +280,7 @@ def test_compile_circuit_no_routing_attempts():
with pytest.raises(AssertionError) as e:
cirq.contrib.quantum_volume.compile_circuit(
model_circuit,
device_graph=ccr.xmon_device_to_graph(cirq.google.Bristlecone),
device_graph=ccr.gridqubits_to_graph_device(TestDevice().qubits),
routing_attempts=0,
)
assert e.match('Unable to get routing for circuit')
Expand All @@ -288,7 +292,7 @@ def test_calculate_quantum_volume_result():
num_qubits=3,
depth=3,
num_circuits=1,
device_or_qubits=cirq.google.Bristlecone,
device_graph=ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(3, 3)),
samplers=[cirq.Simulator()],
routing_attempts=2,
random_state=1,
Expand All @@ -307,11 +311,12 @@ def test_calculate_quantum_volume_result_with_device_graph():
"""Test that running the main loop routes the circuit onto the given device
graph"""
device_qubits = [cirq.GridQubit(i, j) for i in range(2) for j in range(3)]

results = cirq.contrib.quantum_volume.calculate_quantum_volume(
num_qubits=3,
depth=3,
num_circuits=1,
device_or_qubits=device_qubits,
device_graph=ccr.gridqubits_to_graph_device(device_qubits),
samplers=[cirq.Simulator()],
routing_attempts=2,
random_state=1,
Expand All @@ -333,7 +338,7 @@ def test_calculate_quantum_volume_loop():
num_circuits=1,
routing_attempts=2,
random_state=1,
device_or_qubits=cirq.google.Bristlecone,
device_graph=ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(3, 3)),
samplers=[cirq.Simulator()],
)

Expand All @@ -349,7 +354,45 @@ def test_calculate_quantum_volume_loop_with_readout_correction():
num_circuits=1,
routing_attempts=2,
random_state=1,
device_or_qubits=cirq.google.Bristlecone,
device_graph=ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(3, 3)),
samplers=[cirq.Simulator()],
add_readout_error_correction=True,
)


def test_deprecated():
with cirq.testing.assert_deprecated(
"device_or_qubits", "use device_graph instead", deadline="v0.12"
):
# pylint: disable=unexpected-keyword-arg
# pylint: disable=missing-kwoa
cirq.contrib.quantum_volume.calculate_quantum_volume(
num_qubits=4,
depth=4,
num_circuits=1,
routing_attempts=2,
random_state=1,
device_or_qubits=cirq.GridQubit.rect(3, 3),
samplers=[cirq.Simulator()],
add_readout_error_correction=True,
)
# pylint: enable=unexpected-keyword-arg
# pylint: enable=missing-kwoa

with cirq.testing.assert_deprecated(
"device_or_qubits", "use device_graph instead", deadline="v0.12"
):
# pylint: disable=unexpected-keyword-arg
# pylint: disable=missing-kwoa
cirq.contrib.quantum_volume.calculate_quantum_volume(
num_qubits=4,
depth=4,
num_circuits=1,
routing_attempts=2,
random_state=1,
device_or_qubits=TestDevice(),
samplers=[cirq.Simulator()],
add_readout_error_correction=True,
)
# pylint: enable=unexpected-keyword-arg
# pylint: enable=missing-kwoa
8 changes: 5 additions & 3 deletions cirq/contrib/routing/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
# limitations under the License.

import itertools
from typing import Iterable, Tuple, Dict
from typing import Iterable, Tuple, Dict, Any

import networkx as nx

import cirq
from cirq._compat import deprecated


def xmon_device_to_graph(device: cirq.google.XmonDevice) -> nx.Graph:
@deprecated(deadline="v0.12", fix="use gridqubits_to_graph_device(device.qubits) instead")
def xmon_device_to_graph(device: Any) -> nx.Graph:
"""Gets the graph of an XmonDevice."""
return gridqubits_to_graph_device(device.qubits)

Expand Down Expand Up @@ -62,7 +64,7 @@ def nx_qubit_layout(graph: nx.Graph) -> Dict[cirq.Qid, Tuple[float, float]]:
>>> import matplotlib.pyplot as plt
>>> # Clear plot state to prevent issues with pyplot dimensionality.
>>> plt.clf()
>>> g = ccr.xmon_device_to_graph(cirq.google.Foxtail)
>>> g = ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(4,5))
>>> pos = ccr.nx_qubit_layout(g)
>>> nx.draw_networkx(g, pos=pos)
Expand Down
19 changes: 12 additions & 7 deletions cirq/contrib/routing/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@


def test_xmon_device_to_graph():
foxtail_graph = ccr.xmon_device_to_graph(cirq.google.Foxtail)
two_by_eleven_grid_graph = ccr.get_grid_device_graph(2, 11)
assert foxtail_graph.nodes == two_by_eleven_grid_graph.nodes
assert foxtail_graph.edges() == two_by_eleven_grid_graph.edges()
with cirq.testing.assert_deprecated("gridqubits_to_graph_device", deadline="v0.12"):

class TestDevice:
qubits = cirq.GridQubit.rect(2, 11)

foxtail_graph = ccr.xmon_device_to_graph(TestDevice())
two_by_eleven_grid_graph = ccr.get_grid_device_graph(2, 11)
assert foxtail_graph.nodes == two_by_eleven_grid_graph.nodes
assert foxtail_graph.edges() == two_by_eleven_grid_graph.edges()


@pytest.mark.parametrize('n_qubits', (2, 5, 11))
Expand All @@ -36,9 +41,9 @@ def test_get_linear_device_graph(n_qubits):


def test_nx_qubit_layout():
foxtail_graph = ccr.xmon_device_to_graph(cirq.google.Foxtail)
pos = ccr.nx_qubit_layout(foxtail_graph)
assert len(pos) == len(foxtail_graph)
grid_qubit_graph = ccr.gridqubits_to_graph_device(cirq.GridQubit.rect(5, 5))
pos = ccr.nx_qubit_layout(grid_qubit_graph)
assert len(pos) == len(grid_qubit_graph)
for k, (x, y) in pos.items():
assert x == k.col
assert y == -k.row
Expand Down
1 change: 0 additions & 1 deletion cirq/protocols/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import datetime

import datetime
import io
Expand Down
7 changes: 6 additions & 1 deletion dev_tools/notebooks/isolated_notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@
# these notebooks rely on features that are not released yet
# after every release we should raise a PR and empty out this list
# note that these notebooks are still tested in dev_tools/notebook_test.py
NOTEBOOKS_DEPENDING_ON_UNRELEASED_FEATURES = []
NOTEBOOKS_DEPENDING_ON_UNRELEASED_FEATURES = [
# these notebooks now use cirq.contrib.calculate_quantum_volume(...device_qubits...)
# the device_or_qubits parameter is deprecated
'examples/advanced/quantum_volume_routing.ipynb',
'examples/advanced/quantum_volume_errors.ipynb',
]

# By default all notebooks should be tested, however, this list contains exceptions to the rule
# please always add a reason for skipping.
Expand Down
2 changes: 1 addition & 1 deletion docs/qcvv/isolated_xeb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
" device = cg.get_engine_device(device_name)\n",
"\n",
" import cirq.contrib.routing as ccr\n",
" graph = ccr.xmon_device_to_graph(device)\n",
" graph = ccr.gridqubits_to_graph_device(device.qubits)\n",
" pos = {q: (q.row, q.col) for q in graph.nodes}\n",
" nx.draw_networkx(graph, pos=pos)"
]
Expand Down
2 changes: 1 addition & 1 deletion docs/qcvv/parallel_xeb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
" sampler = cg.get_engine_sampler(device_name, gate_set_name='sqrt_iswap')\n",
" device = cg.get_engine_device(device_name)\n",
" qubits = sorted(device.qubits)\n",
" graph = ccr.xmon_device_to_graph(device)\n",
" graph = ccr.gridqubits_to_graph_device(device.qubits)\n",
"\n",
"\n",
"pos = {q: (q.row, q.col) for q in qubits}\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/advanced/quantum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import argparse
import sys
from typing import Optional, List, cast, Callable, Dict, Tuple

from cirq.contrib.quantum_volume import calculate_quantum_volume
import cirq
from cirq.contrib.quantum_volume import calculate_quantum_volume
from cirq.contrib import routing


def main(*, num_qubits: int, depth: int, num_circuits: int, seed: int, routes: int):
Expand All @@ -48,7 +48,7 @@ def main(*, num_qubits: int, depth: int, num_circuits: int, seed: int, routes: i
depth=depth,
num_circuits=num_circuits,
random_state=seed,
device_or_qubits=device,
device_graph=routing.gridqubits_to_graph_device(device.qubits),
samplers=[cirq.Simulator(), noisy],
routing_attempts=routes,
compiler=compiler,
Expand Down
8 changes: 4 additions & 4 deletions examples/advanced/quantum_volume_errors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
" import cirq\n",
"except ImportError:\n",
" print(\"installing cirq...\")\n",
" !pip install --quiet cirq\n",
" !pip install --quiet cirq --pre\n",
" print(\"installed cirq.\")"
]
},
Expand Down Expand Up @@ -54,7 +54,7 @@
"# Run the Quantum Volume algorithm over the above parameters.\n",
"\n",
"import numpy as np\n",
"from cirq.contrib import quantum_volume\n",
"from cirq.contrib import quantum_volume, routing\n",
"\n",
"errors = np.logspace(-1, -4, num=num_samplers)\n",
"samplers = [\n",
Expand All @@ -65,7 +65,7 @@
"result = quantum_volume.calculate_quantum_volume(num_circuits=num_circuits,\n",
" depth=depth,\n",
" num_qubits=depth,\n",
" device_or_qubits=device,\n",
" device_graph=routing.gridqubits_to_graph_device(device.qubits),\n",
" samplers=samplers,\n",
" compiler=compiler)"
]
Expand Down Expand Up @@ -127,4 +127,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}

0 comments on commit 745790e

Please sign in to comment.