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

Fixes for mypy; update pytket requirement #34

Merged
merged 8 commits into from
Sep 7, 2023
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
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
~~~~~~~~~

Unreleased
----------

* Update pytket version requirement to 1.19.

0.29.0 (June 2023)
------------------

Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace_packages = True
check_untyped_defs = True

warn_redundant_casts = True
warn_unused_ignores = False
warn_unused_ignores = True
warn_no_return = False
warn_return_any = True
warn_unreachable = True
Expand Down
2 changes: 1 addition & 1 deletion pytket/extensions/cirq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Module for conversion between Google Cirq and tket primitives."""

# _metadata.py is copied to the folder after installation.
from ._metadata import __extension_version__, __extension_name__ # type: ignore
from ._metadata import __extension_version__, __extension_name__
from .backends.cirq_convert import cirq_to_tk, tk_to_cirq, process_characterisation
from .backends import (
CirqStateSampleBackend,
Expand Down
18 changes: 8 additions & 10 deletions pytket/extensions/cirq/backends/cirq.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
from cirq.devices import NOISE_MODEL_LIKE
from cirq.circuits import Circuit as CirqCircuit

from pytket.circuit import Circuit, OpType, Qubit # type: ignore
from pytket.transform import Transform # type: ignore
from pytket.passes import ( # type: ignore
from pytket.circuit import Circuit, OpType, Qubit
from pytket.transform import Transform
from pytket.passes import (
BasePass,
auto_rebase_pass,
SequencePass,
Expand All @@ -41,8 +41,8 @@
RemoveRedundancies,
FullPeepholeOptimise,
)
from pytket._tket.circuit._library import _TK1_to_PhasedXRz, _CX # type: ignore
from pytket.predicates import ( # type: ignore
from pytket.circuit_library import _TK1_to_PhasedXRz, _CX
from pytket.predicates import (
GateSetPredicate,
NoClassicalControlPredicate,
NoFastFeedforwardPredicate,
Expand All @@ -54,7 +54,7 @@
from pytket.backends.resulthandle import _ResultIdTuple
from pytket.utils.results import KwargTypes
from pytket.utils.outcomearray import OutcomeArray
from .cirq_convert import tk_to_cirq # type: ignore
from .cirq_convert import tk_to_cirq
from .cirq_utils import _get_default_uids


Expand Down Expand Up @@ -272,7 +272,6 @@ def process_circuits(
valid_check: bool = True,
**kwargs: KwargTypes,
) -> List[ResultHandle]:

if n_shots is not None:
raise ValueError("`n_shots` argument is invalid for _CirqSimBackend")

Expand Down Expand Up @@ -329,7 +328,6 @@ def process_circuits_moments(
valid_check: bool = True,
**kwargs: KwargTypes,
) -> List[ResultHandle]:

"""
Submit circuits to the backend for running. The results will be stored
in the backend's result cache to be retrieved by the corresponding
Expand All @@ -354,7 +352,7 @@ def process_circuits_moments(
handle = ResultHandle(str(uuid4()), i)
handle_list.append(handle)
backres = self._run_circuit_moments(circuit)
self._cache[handle] = {"result": backres} # type: ignore
self._cache[handle] = {"result": backres}

return handle_list

Expand Down Expand Up @@ -571,5 +569,5 @@ def _tk1_to_phasedxrz_clifford(a: float, b: float, c: float) -> Circuit:
OpType.CZ,
},
_CX(),
_tk1_to_phasedxrz_clifford,
_tk1_to_phasedxrz_clifford, # type: ignore
)
23 changes: 12 additions & 11 deletions pytket/extensions/cirq/backends/cirq_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
from cirq.devices import LineQubit, GridQubit
import cirq.ops
import cirq_google
from sympy import pi, Basic, Symbol # type: ignore
from sympy import pi, Basic, Symbol

from pytket.circuit import Circuit, OpType, Qubit, Bit, Node # type: ignore
from pytket.architecture import Architecture # type: ignore
from pytket.circuit import Circuit, OpType, Qubit, Bit, Node
from pytket.architecture import Architecture

# For translating cirq circuits to tket circuits
cirq_common = cirq.ops.common_gates
Expand Down Expand Up @@ -182,9 +182,10 @@ def cirq_to_tk(circuit: cirq.circuits.Circuit) -> Circuit:
elif isinstance(gate, cirq_common.MeasurementGate):
# Adding "_b" to the bit uid since for cirq.NamedQubit,
# the gate.key is equal to the qubit id (the qubit name)
uid = Bit(gate.key + "_b")
tkcirc.add_bit(uid)
tkcirc.Measure(*qb_lst, uid)
bitid = Bit(gate.key + "_b")
tkcirc.add_bit(bitid)
assert len(qb_lst) == 1
tkcirc.Measure(qb_lst[0], bitid)
continue
elif isinstance(gate, cirq.ops.PhasedXPowGate):
optype = OpType.PhasedX
Expand All @@ -205,10 +206,10 @@ def cirq_to_tk(circuit: cirq.circuits.Circuit) -> Circuit:
"Operation not supported by tket: " + str(op.gate)
) from error
if apply_in_parallel:
for qb in qb_lst:
tkcirc.add_gate(optype, params, [qb])
for qbit in qb_lst:
tkcirc.add_gate(optype, params, [qbit]) # type: ignore
else:
tkcirc.add_gate(optype, params, qb_lst)
tkcirc.add_gate(optype, params, qb_lst) # type: ignore
return tkcirc


Expand All @@ -224,7 +225,7 @@ def tk_to_cirq(tkcirc: Circuit, copy_all_qubits: bool = False) -> cirq.circuits.
for q in tkcirc.qubits:
tkcirc.add_gate(OpType.noop, [q])

qmap = {}
qmap: Dict[Qubit, Union[cirq.ops.NamedQubit, LineQubit, GridQubit]] = {}
line_name = None
grid_name = None
# Since Cirq can only support registers of up to 2 dimensions, we explicitly
Expand Down Expand Up @@ -324,7 +325,7 @@ def process_characterisation(
:return: A dictionary containing device characteristics
"""
data = device.metadata
qubits: FrozenSet[GridQubit] = data.qubit_set # type: ignore
qubits: FrozenSet[GridQubit] = data.qubit_set
qubit_graph = data.nx_graph

qb_map = {q: Node("q", q.row, q.col) for q in qubits}
Expand Down
3 changes: 1 addition & 2 deletions pytket/extensions/cirq/backends/cirq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
from cirq.ops import QubitOrder, MeasurementGate, NamedQubit
from cirq.devices import LineQubit, GridQubit
from cirq.protocols import is_measurement
from pytket.circuit import Circuit, Qubit, Bit # type: ignore
from pytket.circuit import Circuit, Qubit, Bit


def _get_default_uids(
cirq_circuit: CirqCircuit, tket_circuit: Circuit
) -> Tuple[List[Bit], List[Qubit]]:

if len(tket_circuit.qubit_readout) == 0:
return [], tket_circuit.qubits
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
packages=find_namespace_packages(include=["pytket.*"]),
include_package_data=True,
install_requires=[
"pytket ~= 1.16",
"pytket ~= 1.19",
"cirq-core ~= 1.0",
"cirq-google ~= 1.0",
"protobuf ~= 3.20, < 4.0",
Expand Down
8 changes: 4 additions & 4 deletions tests/cirq_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
from cirq.devices import LineQubit, GridQubit
from cirq.ops import NamedQubit

from pytket import OpType # type: ignore
from pytket.circuit import OpType
from pytket.extensions.cirq import cirq_to_tk, tk_to_cirq, process_characterisation
from pytket.architecture import Architecture # type: ignore
from pytket.architecture import Architecture


def get_match_circuit(
cirq_qubit_type: str = "LineQubit", radian_gates: bool = False
) -> cirq.Circuit:
if cirq_qubit_type == "LineQubit":
qubits = [LineQubit(i) for i in range(9)] # type: ignore
qubits = [LineQubit(i) for i in range(9)]
if cirq_qubit_type == "GridQubit":
qubits = GridQubit.square(3) # type: ignore
if cirq_qubit_type == "NamedQubit":
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_device() -> None:
@pytest.mark.parametrize("cirq_qubit_type", ["LineQubit", "GridQubit", "NamedQubit"])
def test_parallel_ops(cirq_qubit_type: str) -> None:
if cirq_qubit_type == "LineQubit":
q0, q1, q2 = [LineQubit(i) for i in range(3)] # type: ignore
q0, q1, q2 = [LineQubit(i) for i in range(3)]
if cirq_qubit_type == "GridQubit":
q0, q1, q2 = GridQubit.rect(rows=1, cols=3) # type: ignore
if cirq_qubit_type == "NamedQubit":
Expand Down
2 changes: 1 addition & 1 deletion tests/test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pytest
pytest-timeout ~= 1.4.2
pytest-timeout
hypothesis
requests_mock
18 changes: 9 additions & 9 deletions tests/test_cirq_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import numpy as np
import pytest
from _pytest.fixtures import FixtureRequest
from cirq.contrib.noise_models import DepolarizingNoiseModel # type: ignore
from cirq.contrib.noise_models import DepolarizingNoiseModel

from pytket.extensions.cirq.backends.cirq import (
CirqDensityMatrixSampleBackend,
Expand All @@ -32,21 +32,21 @@
_CirqSimBackend,
_CirqBaseBackend,
)
from pytket.circuit import Circuit, Qubit, Bit # type: ignore
from pytket.circuit import Circuit, Qubit, Bit
from pytket.backends import StatusEnum
from pytket.predicates import GateSetPredicate # type: ignore
from pytket.predicates import GateSetPredicate


@pytest.fixture(
name="qubit_readout_circ", params=["LineQubit", "GridQubit", "NamedQubit"]
)
def fixture_qubit_readout_circ(request: FixtureRequest) -> Circuit:
qubits = []
if request.param == "LineQubit": # type: ignore
if request.param == "LineQubit":
qubits = [Qubit("q", x) for x in range(4)]
if request.param == "GridQubit": # type: ignore
if request.param == "GridQubit":
qubits = [Qubit("g", row=r, col=c) for r in range(2) for c in range(2)]
if request.param == "NamedQubit": # type: ignore
if request.param == "NamedQubit":
qubits = [Qubit("qubit" + str(x)) for x in range(4)]
circ = Circuit()
for q in qubits:
Expand Down Expand Up @@ -313,8 +313,8 @@ def test_clifford_compilation() -> None:

def test_noisy_simulator_backends() -> None:
nm = DepolarizingNoiseModel(depol_prob=0.01)
sim_backend = CirqDensityMatrixSimBackend(noise_model=nm) # type: ignore
sample_backend = CirqDensityMatrixSampleBackend(noise_model=nm) # type: ignore
sim_backend = CirqDensityMatrixSimBackend(noise_model=nm)
sample_backend = CirqDensityMatrixSampleBackend(noise_model=nm)

assert sim_backend._simulator.noise == nm
assert sample_backend._simulator.noise == nm
Expand All @@ -332,7 +332,7 @@ def test_shots_bits_edgecases(n_shots, n_bits) -> None:
h = cirq_backend.process_circuit(c, n_shots)
res = cirq_backend.get_result(h)

correct_shots = np.zeros((n_shots, n_bits), dtype=int)
correct_shots = np.zeros((n_shots, n_bits), dtype=int) # type: ignore
correct_shape = (n_shots, n_bits)
correct_counts = Counter({(0,) * n_bits: n_shots})
# BackendResult
Expand Down