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

Move quirk url importing out of contrib into interop #2512

Merged
merged 4 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
neutral_atoms,
api,
google,
interop,
# Applications
experiments,
# Extra (nothing should depend on these)
Expand Down Expand Up @@ -98,6 +99,11 @@
xeb_fidelity,
)

from cirq.interop import (
quirk_json_to_circuit,
quirk_url_to_circuit,
)

from cirq.linalg import (
all_near_zero,
all_near_zero_mod,
Expand Down
12 changes: 0 additions & 12 deletions cirq/contrib/quirk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,5 @@

"""Converts cirq circuits into quirk circuits."""

# Imports from cells are only to ensure operation reprs work correctly.
from cirq.contrib.quirk.cells import (
QuirkArithmeticOperation,
QuirkInputRotationOperation,
QuirkQubitPermutationGate,
)

from cirq.contrib.quirk.export_to_quirk import (
circuit_to_quirk_url,)

from cirq.contrib.quirk.url_to_circuit import (
quirk_json_to_circuit,
quirk_url_to_circuit,
)
6 changes: 6 additions & 0 deletions cirq/interop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Package containing code for interoperating with other quantum software."""

from cirq.interop.quirk import (
quirk_json_to_circuit,
quirk_url_to_circuit,
)
31 changes: 31 additions & 0 deletions cirq/interop/quirk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2018 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
#
# http://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.
"""Code related to interoperating with Quirk, a drag-and-drop circuit simulator.

References:
https://github.com/strilanc/quirk - Quirk source code.
https://algassert.com/quirk - Live version of Quirk.
"""

# Imports from cells are only to ensure operation reprs work correctly.
from cirq.interop.quirk.cells import (
QuirkArithmeticOperation,
QuirkInputRotationOperation,
QuirkQubitPermutationGate,
)

from cirq.interop.quirk.url_to_circuit import (
quirk_json_to_circuit,
quirk_url_to_circuit,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
# limitations under the License.
"""This module defines building blocks for parsing Quirk circuits."""

from cirq.contrib.quirk.cells.all_cells import (
from cirq.interop.quirk.cells.all_cells import (
generate_all_quirk_cell_makers,)

from cirq.contrib.quirk.cells.cell import (
from cirq.interop.quirk.cells.cell import (
Cell,
CellMaker,
CellMakerArgs,
ExplicitOperationsCell,
)

from cirq.contrib.quirk.cells.composite_cell import (
from cirq.interop.quirk.cells.composite_cell import (
CompositeCell,)

from cirq.contrib.quirk.cells.qubit_permutation_cells import (
from cirq.interop.quirk.cells.qubit_permutation_cells import (
QuirkQubitPermutationGate,)

from cirq.contrib.quirk.cells.arithmetic_cells import (
from cirq.interop.quirk.cells.arithmetic_cells import (
QuirkArithmeticOperation,)

from cirq.contrib.quirk.cells.input_rotation_cells import (
from cirq.interop.quirk.cells.input_rotation_cells import (
QuirkInputRotationOperation,)

import cirq.contrib.quirk.cells.swap_cell
import cirq.contrib.quirk.cells.control_cells
import cirq.interop.quirk.cells.swap_cell
import cirq.interop.quirk.cells.control_cells
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@
# limitations under the License.
from typing import Iterator

from cirq.contrib.quirk.cells.arithmetic_cells import (
from cirq.interop.quirk.cells.arithmetic_cells import (
generate_all_arithmetic_cell_makers)
from cirq.contrib.quirk.cells.cell import CellMaker
from cirq.contrib.quirk.cells.control_cells import (
from cirq.interop.quirk.cells.cell import CellMaker
from cirq.interop.quirk.cells.control_cells import (
generate_all_control_cell_makers)
from cirq.contrib.quirk.cells.frequency_space_cells import (
from cirq.interop.quirk.cells.frequency_space_cells import (
generate_all_frequency_space_cell_makers)
from cirq.contrib.quirk.cells.ignored_cells import (
from cirq.interop.quirk.cells.ignored_cells import (
generate_all_ignored_cell_makers)
from cirq.contrib.quirk.cells.input_cells import (generate_all_input_cell_makers
from cirq.interop.quirk.cells.input_cells import (generate_all_input_cell_makers
)
from cirq.contrib.quirk.cells.input_rotation_cells import (
from cirq.interop.quirk.cells.input_rotation_cells import (
generate_all_input_rotation_cell_makers)
from cirq.contrib.quirk.cells.measurement_cells import (
from cirq.interop.quirk.cells.measurement_cells import (
generate_all_measurement_cell_makers)
from cirq.contrib.quirk.cells.qubit_permutation_cells import (
from cirq.interop.quirk.cells.qubit_permutation_cells import (
generate_all_qubit_permutation_cell_makers)
from cirq.contrib.quirk.cells.scalar_cells import (
from cirq.interop.quirk.cells.scalar_cells import (
generate_all_scalar_cell_makers)
from cirq.contrib.quirk.cells.single_qubit_rotation_cells import (
from cirq.interop.quirk.cells.single_qubit_rotation_cells import (
generate_all_single_qubit_rotation_cell_makers)
from cirq.contrib.quirk.cells.swap_cell import (generate_all_swap_cell_makers)
from cirq.contrib.quirk.cells.unsupported_cells import (
from cirq.interop.quirk.cells.swap_cell import (generate_all_swap_cell_makers)
from cirq.interop.quirk.cells.unsupported_cells import (
generate_all_unsupported_cell_makers)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Tuple, Any, cast, List, Dict, TYPE_CHECKING)

from cirq import ops, value
from cirq.contrib.quirk.cells.cell import Cell, CellMaker, CELL_SIZES
from cirq.interop.quirk.cells.cell import Cell, CellMaker, CELL_SIZES

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -117,7 +117,7 @@ def _circuit_diagram_info_(self, args: 'cirq.CircuitDiagramInfoArgs'
return result

def __repr__(self) -> str:
return ('cirq.contrib.quirk.QuirkArithmeticOperation(\n'
return ('cirq.interop.quirk.QuirkArithmeticOperation(\n'
f' {repr(self.identifier)},\n'
f' target={repr(self.target)},\n'
f' inputs={_indented_list_lines_repr(self.inputs)},\n'
Expand Down Expand Up @@ -176,7 +176,7 @@ def _value_equality_values_(self):
return self.identifier, self.target, self.inputs

def __repr__(self):
return (f'cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell('
return (f'cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell('
f'\n {self.identifier!r},'
f'\n {self.target!r},'
f'\n {self.inputs!r})')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import pytest

import cirq
from cirq.contrib.quirk.cells import arithmetic_cells
from cirq.contrib.quirk.cells.testing import assert_url_to_circuit_returns
from cirq.contrib.quirk.url_to_circuit import quirk_url_to_circuit
from cirq.interop.quirk.cells import arithmetic_cells
from cirq.interop.quirk.cells.testing import assert_url_to_circuit_returns
from cirq import quirk_url_to_circuit


def test_arithmetic_comparison_gates():
Expand Down Expand Up @@ -470,7 +470,7 @@ def test_repr():
cirq.testing.assert_equivalent_repr(op)

cirq.testing.assert_equivalent_repr(
cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', cirq.LineQubit.range(2), [cirq.LineQubit.range(2, 5)]))


Expand Down Expand Up @@ -559,25 +559,25 @@ def test_with_line_qubits_mapped_to():
a2, b2, c2, d2, e2 = cirq.NamedQubit.range(5, prefix='p')

# After assigned to qubit register.
cell = cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
cell = cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', [a, b], [(c, d, e)])
mapped_cell = cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
mapped_cell = cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', [a2, b2], [(c2, d2, e2)])
assert cell != mapped_cell
assert cell.with_line_qubits_mapped_to([a2, b2, c2, d2, e2]) == mapped_cell

# Before assigned.
cell = cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
cell = cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', [a, b], [None])
mapped_cell = cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
mapped_cell = cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', [a2, b2], [None])
assert cell != mapped_cell
assert cell.with_line_qubits_mapped_to([a2, b2, c2, d2, e2]) == mapped_cell

# After assigned to classical constant.
cell = cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
cell = cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', [a, b], [42])
mapped_cell = cirq.contrib.quirk.cells.arithmetic_cells.ArithmeticCell(
mapped_cell = cirq.interop.quirk.cells.arithmetic_cells.ArithmeticCell(
'+=A2', [a2, b2], [42])
assert cell != mapped_cell
assert cell.with_line_qubits_mapped_to([a2, b2, c2, d2, e2]) == mapped_cell
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def controlled_by(self, qubit: 'cirq.Qid') -> 'ExplicitOperationsCell':
Attributes:
identifier: A string that identifies the cell type, such as "X" or "QFT3".
size: The height of the operation. The number of qubits it covers.
maker: A function that takes a `cirq.contrib.quirk.cells.CellMakerArgs` and
returns either a `cirq.Operation` or a `cirq.contrib.quirk.cells.Cell`.
maker: A function that takes a `cirq.interop.quirk.cells.CellMakerArgs` and
returns either a `cirq.Operation` or a `cirq.interop.quirk.cells.Cell`.
Returning a cell is more flexible, because cells can modify other cells
in the same column before producing operations, whereas returning an
operation is simple.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import pytest

import cirq
from cirq.contrib.quirk.cells.cell import Cell, ExplicitOperationsCell
from cirq.interop.quirk.cells.cell import Cell, ExplicitOperationsCell


def test_cell_defaults():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Iterable, TypeVar, Union, Sequence

from cirq import circuits
from cirq.contrib.quirk.cells.cell import Cell
from cirq.interop.quirk.cells.cell import Cell

if TYPE_CHECKING:
import cirq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import pytest

import cirq
from cirq.contrib.quirk.cells.testing import assert_url_to_circuit_returns
from cirq.contrib.quirk import quirk_json_to_circuit
from cirq.contrib.quirk.cells.composite_cell import _iterator_to_iterable
from cirq import quirk_json_to_circuit
from cirq.interop.quirk.cells.testing import assert_url_to_circuit_returns
from cirq.interop.quirk.cells.composite_cell import _iterator_to_iterable


def test_iterator_to_iterable():
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_custom_circuit_gate():
'{"cols":[["~a5ls"]],"gates":[{"id":"~a5ls",'
'"circuit":{"cols":[["inputA1","+=A1"]]}}]}',
cirq.Circuit(
cirq.contrib.quirk.QuirkArithmeticOperation('+=A1',
cirq.interop.quirk.QuirkArithmeticOperation('+=A1',
target=[b],
inputs=[[a]])))

Expand All @@ -109,7 +109,7 @@ def test_custom_circuit_gate():
'{"cols":[["inputA1","~r79k"]],"gates":[{"id":"~r79k",'
'"circuit":{"cols":[["+=A1"]]}}]}',
cirq.Circuit(
cirq.contrib.quirk.QuirkArithmeticOperation('+=A1',
cirq.interop.quirk.QuirkArithmeticOperation('+=A1',
target=[b],
inputs=[[a]])))

Expand Down Expand Up @@ -137,13 +137,13 @@ def test_custom_circuit_gate():
'{"cols":[["~q1fh",1,1,"inputA2"]],"gates":[{"id":"~q1fh",'
'"circuit":{"cols":[["+=A2"],[1,"+=A2"],[1,"+=A2"]]}}]}',
cirq.Circuit(
cirq.contrib.quirk.QuirkArithmeticOperation('+=A2',
cirq.interop.quirk.QuirkArithmeticOperation('+=A2',
target=[a, b],
inputs=[[d, e]]),
cirq.contrib.quirk.QuirkArithmeticOperation('+=A2',
cirq.interop.quirk.QuirkArithmeticOperation('+=A2',
target=[b, c],
inputs=[[d, e]]),
cirq.contrib.quirk.QuirkArithmeticOperation('+=A2',
cirq.interop.quirk.QuirkArithmeticOperation('+=A2',
target=[b, c],
inputs=[[d, e]]),
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Optional, List, Iterable, TYPE_CHECKING, Union, Iterator

from cirq import ops, value
from cirq.contrib.quirk.cells.cell import Cell, CellMaker
from cirq.interop.quirk.cells.cell import Cell, CellMaker

if TYPE_CHECKING:
import cirq
Expand All @@ -34,7 +34,7 @@ def _value_equality_values_(self):
return self.qubit, self._basis_change

def __repr__(self):
return (f'cirq.contrib.quirk.cells.control_cells.ControlCell('
return (f'cirq.interop.quirk.cells.control_cells.ControlCell('
f'\n {self.qubit!r},'
f'\n {self._basis_change!r})')

Expand Down Expand Up @@ -75,7 +75,7 @@ def _value_equality_values_(self):
return self.qubits, self._basis_change

def __repr__(self):
return (f'cirq.contrib.quirk.cells.control_cells.ParityControlCell('
return (f'cirq.interop.quirk.cells.control_cells.ParityControlCell('
f'\n {self.qubits!r},'
f'\n {self._basis_change!r})')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import cirq
from cirq.contrib.quirk.cells.testing import assert_url_to_circuit_returns
from cirq.interop.quirk.cells.testing import assert_url_to_circuit_returns


def test_controls():
Expand Down Expand Up @@ -168,9 +168,9 @@ def test_parity_controls():
def test_control_with_line_qubits_mapped_to():
a, b = cirq.LineQubit.range(2)
a2, b2 = cirq.NamedQubit.range(2, prefix='q')
cell = cirq.contrib.quirk.cells.control_cells.ControlCell(
cell = cirq.interop.quirk.cells.control_cells.ControlCell(
a, [cirq.Y(b)**0.5])
mapped_cell = cirq.contrib.quirk.cells.control_cells.ControlCell(
mapped_cell = cirq.interop.quirk.cells.control_cells.ControlCell(
a2, [cirq.Y(b2)**0.5])
assert cell != mapped_cell
assert cell.with_line_qubits_mapped_to([a2, b2]) == mapped_cell
Expand All @@ -179,9 +179,9 @@ def test_control_with_line_qubits_mapped_to():
def test_parity_control_with_line_qubits_mapped_to():
a, b, c = cirq.LineQubit.range(3)
a2, b2, c2 = cirq.NamedQubit.range(3, prefix='q')
cell = cirq.contrib.quirk.cells.control_cells.ParityControlCell(
cell = cirq.interop.quirk.cells.control_cells.ParityControlCell(
[a, b], [cirq.Y(c)**0.5])
mapped_cell = cirq.contrib.quirk.cells.control_cells.ParityControlCell(
mapped_cell = cirq.interop.quirk.cells.control_cells.ParityControlCell(
[a2, b2], [cirq.Y(c2)**0.5])
assert cell != mapped_cell
assert cell.with_line_qubits_mapped_to([a2, b2, c2]) == mapped_cell
Expand All @@ -190,7 +190,7 @@ def test_parity_control_with_line_qubits_mapped_to():
def test_repr():
a, b, c = cirq.LineQubit.range(3)
cirq.testing.assert_equivalent_repr(
cirq.contrib.quirk.cells.control_cells.ControlCell(a, [cirq.Y(b)**0.5]))
cirq.interop.quirk.cells.control_cells.ControlCell(a, [cirq.Y(b)**0.5]))
cirq.testing.assert_equivalent_repr(
cirq.contrib.quirk.cells.control_cells.ParityControlCell(
cirq.interop.quirk.cells.control_cells.ParityControlCell(
[a, b], [cirq.Y(c)**0.5]))
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sympy

import cirq
from cirq.contrib.quirk.cells.cell import (
from cirq.interop.quirk.cells.cell import (
CellMaker,
CELL_SIZES,
ExplicitOperationsCell,
Expand Down
Loading