Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #426 from CQCL/release/pytket-1.3.0
Browse files Browse the repository at this point in the history
Extension release for pytket 1.3.0
  • Loading branch information
cqc-melf authored Jun 16, 2022
2 parents 66da3a6 + cd9addd commit ec16228
Show file tree
Hide file tree
Showing 59 changed files with 1,180 additions and 261 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ jobs:
with:
python-version: '3.9'
- name: Build and test including remote checks (3.9)
if: github.event_name == 'pull_request' || github.event_name == 'release' || contains(github.ref, 'refs/heads/wheel')
if: |
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)
|| github.event_name == 'release'
|| contains(github.ref, 'refs/heads/wheel')
run: |
./.github/workflows/build-test nomypy
env:
Expand Down Expand Up @@ -191,7 +194,7 @@ jobs:
with:
python-version: '3.9'
- name: Build and test including remote checks and mypy (3.9)
if: github.event_name == 'pull_request'
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
run: |
./.github/workflows/build-test mypy
env:
Expand Down Expand Up @@ -257,7 +260,7 @@ jobs:
with:
python-version: '3.9'
- name: Build and test including remote checks (3.9)
if: github.event_name == 'pull_request'
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
shell: bash
run: |
./.github/workflows/build-test nomypy
Expand Down
2 changes: 1 addition & 1 deletion modules/pytket-aqt/_metadata.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__extension_version__ = "0.20.0"
__extension_version__ = "0.21.0"
__extension_name__ = "pytket-aqt"
5 changes: 5 additions & 0 deletions modules/pytket-aqt/docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
~~~~~~~~~

0.21.0 (June 2022)
------------------

* Updated pytket version requirement to 1.3.

0.20.0 (May 2022)
-----------------

Expand Down
2 changes: 1 addition & 1 deletion modules/pytket-aqt/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
license="Apache 2",
packages=find_namespace_packages(include=["pytket.*"]),
include_package_data=True,
install_requires=["pytket ~= 1.2", "requests ~= 2.22", "types-requests"],
install_requires=["pytket ~= 1.3", "requests ~= 2.22", "types-requests"],
classifiers=[
"Environment :: Console",
"Programming Language :: Python :: 3.8",
Expand Down
2 changes: 1 addition & 1 deletion modules/pytket-braket/_metadata.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__extension_version__ = "0.19.1"
__extension_version__ = "0.20.0"
__extension_name__ = "pytket-braket"
3 changes: 3 additions & 0 deletions modules/pytket-braket/docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ API documentation

.. automodule:: pytket.extensions.braket.backends.config
:members:

.. automodule:: pytket.extensions.braket.braket_convert
:members: tk_to_braket, braket_to_tk
5 changes: 5 additions & 0 deletions modules/pytket-braket/docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
~~~~~~~~~

0.20.0 (June 2022)
------------------

* Updated pytket version requirement to 1.3.

0.19.1 (June 2022)
------------------

Expand Down
2 changes: 1 addition & 1 deletion modules/pytket-braket/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
packages=find_namespace_packages(include=["pytket.*"]),
include_package_data=True,
install_requires=[
"pytket ~= 1.2",
"pytket ~= 1.3",
"amazon-braket-sdk~=1.16",
"amazon-braket-schemas~=1.7",
"amazon-braket-default-simulator~=1.5",
Expand Down
1 change: 0 additions & 1 deletion modules/pytket-braket/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def get_authenticated_aws_session(region: Optional[str] = None) -> Optional[AwsS
# Note: this session fixture should be used when creating backends for tests
# where PYTKET_RUN_REMOTE_TESTS is true
region_name = region or os.getenv("PYTKET_REMOTE_BRAKET_REGION")
print(f"region_name: {region_name}")
boto_session = boto3.Session(
aws_access_key_id=os.getenv("PYTKET_REMOTE_BRAKET_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("PYTKET_REMOTE_BRAKET_ACCESS_KEY_SECRET"),
Expand Down
2 changes: 1 addition & 1 deletion modules/pytket-cirq/_metadata.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__extension_version__ = "0.23.0"
__extension_version__ = "0.24.0"
__extension_name__ = "pytket-cirq"
6 changes: 6 additions & 0 deletions modules/pytket-cirq/docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
~~~~~~~~~

0.24.0 (June 2022)
------------------

* `cirq_to_tk()` and `tk_to_cirq()` now properly handle circuits with `NamedQubit`.
* Updated pytket version requirement to 1.3.

0.23.0 (May 2022)
-----------------

Expand Down
15 changes: 5 additions & 10 deletions modules/pytket-cirq/pytket/extensions/cirq/backends/cirq.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
CliffordSimulatorStepResult,
DensityMatrixSimulator,
Simulator,
StateVectorTrialResult,
)

from cirq import ops
Expand Down Expand Up @@ -377,7 +376,6 @@ def package_result(
circuit.all_qubits()
),
)

return BackendResult(state=run.final_state_vector, q_bits=q_bits)

def package_results(
Expand Down Expand Up @@ -469,16 +467,13 @@ def rebase_pass(self) -> BasePass:
def package_result(
self, circuit: CirqCircuit, q_bits: Sequence[Qubit]
) -> BackendResult:
run = cast(
StateVectorTrialResult,
self._simulator.simulate(
circuit,
qubit_order=ops.QubitOrder.as_qubit_order(
ops.QubitOrder.DEFAULT
).order_for(circuit.all_qubits()),
run = self._simulator.simulate(
circuit,
qubit_order=ops.QubitOrder.as_qubit_order(ops.QubitOrder.DEFAULT).order_for(
circuit.all_qubits()
),
)
return BackendResult(state=run.final_state_vector, q_bits=q_bits)
return BackendResult(state=run.final_state.state_vector(), q_bits=q_bits)

def package_results(
self, circuit: CirqCircuit, q_bits: Sequence[Qubit]
Expand Down
32 changes: 25 additions & 7 deletions modules/pytket-cirq/pytket/extensions/cirq/backends/cirq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from typing import Tuple, List, cast
from cirq.circuits import Circuit as CirqCircuit
from cirq.ops import QubitOrder, MeasurementGate
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

Expand All @@ -33,24 +34,41 @@ def _get_default_uids(
cirq_circuit.all_qubits()
)

cirq_measures = [c[1] for c in cirq_circuit.findall_operations(is_measurement)]
ordered_tket_qubits = []
for cirq_qubit in ordered_cirq_qubits:
if isinstance(cirq_qubit, NamedQubit):
ordered_tket_qubits.extend(
[qb for qb in tket_circuit.qubits if qb.reg_name == cirq_qubit.name]
)
if isinstance(cirq_qubit, LineQubit):
ordered_tket_qubits.extend(
[qb for qb in tket_circuit.qubits if qb.index == cirq_qubit.x]
)
if isinstance(cirq_qubit, GridQubit):
ordered_tket_qubits.extend(
[
qb
for qb in tket_circuit.qubits
if (
qb.index[0] == cirq_qubit.row
and qb.index[1] == cirq_qubit.col
)
]
)

cirq_measures = [c[1] for c in cirq_circuit.findall_operations(is_measurement)]
tket_bit_to_qubit_map = {b: q for q, b in tket_circuit.qubit_to_bit_map.items()}

ordered_tket_bits = []
ordered_tket_qubits = []
for cirq_qubit in ordered_cirq_qubits:
for cirq_measure in cirq_measures:
if len(cirq_measure.qubits) > 1:
raise ValueError(
"Cirq Qubit measurement assigned to multiple classical bits."
)
if cirq_measure.qubits[0] == cirq_qubit:
for tket_bit, tket_qubit in tket_bit_to_qubit_map.items():
for tket_bit, _ in tket_bit_to_qubit_map.items():
if cast(MeasurementGate, cirq_measure.gate).key == str(
tket_bit
):
ordered_tket_bits.append(tket_bit)
ordered_tket_qubits.append(tket_qubit)

return (ordered_tket_bits, ordered_tket_qubits)
12 changes: 10 additions & 2 deletions modules/pytket-cirq/pytket/extensions/cirq/cirq_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import List, Dict, FrozenSet, cast, Any, Union
import cmath
from logging import warning
import re
from cirq.devices import LineQubit, GridQubit
import cirq.ops
import cirq_google
Expand Down Expand Up @@ -168,7 +169,9 @@ def cirq_to_tk(circuit: cirq.circuits.Circuit) -> Circuit:
) from error
params: List[Union[float, Basic, Symbol]] = []
elif isinstance(gate, cirq_common.MeasurementGate):
uid = Bit(gate.key)
# 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)
continue
Expand Down Expand Up @@ -252,7 +255,12 @@ def tk_to_cirq(tkcirc: Circuit, copy_all_qubits: bool = False) -> cirq.circuits.
if optype == OpType.Measure:
qid = qmap[command.args[0]]
bit = command.args[1]
cirqop = cirq.ops.measure(qid, key=bit.__repr__())
# Removing the "_b" added to measurement bit registers uids,
# for dealing with NamedQubits
bit_repr = bit.__repr__()
if re.search("_b$", bit_repr):
bit_repr = bit_repr[0:-2]
cirqop = cirq.ops.measure(qid, key=bit_repr)
else:
qids = [qmap[qbit] for qbit in command.args]
params = op.params
Expand Down
3 changes: 2 additions & 1 deletion modules/pytket-cirq/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
packages=find_namespace_packages(include=["pytket.*"]),
include_package_data=True,
install_requires=[
"pytket ~= 1.2",
"pytket ~= 1.3",
"cirq-core ~= 0.14.0",
"cirq-google ~= 0.14.0",
"protobuf ~= 3.20, < 4.0",
],
classifiers=[
"Environment :: Console",
Expand Down
39 changes: 33 additions & 6 deletions modules/pytket-cirq/tests/cirq_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

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

import cirq
import cirq_google
from cirq.circuits import InsertStrategy
from cirq.devices import LineQubit, GridQubit
from cirq.ops import NamedQubit


def get_match_circuit() -> cirq.Circuit:
qubits = [cirq.LineQubit(i) for i in range(9)]
def get_match_circuit(cirq_qubit_type: str = "LineQubit") -> cirq.Circuit:
if cirq_qubit_type == "LineQubit":
qubits = [LineQubit(i) for i in range(9)] # type: ignore
if cirq_qubit_type == "GridQubit":
qubits = GridQubit.square(3) # type: ignore
if cirq_qubit_type == "NamedQubit":
qubits = NamedQubit.range(9, prefix="cirq") # type: ignore

g = cirq.CZPowGate(exponent=0.1)
zz = cirq.ZZPowGate(exponent=0.3)
Expand Down Expand Up @@ -60,8 +68,9 @@ def get_match_circuit() -> cirq.Circuit:
return circ


def test_conversions() -> None:
circ = get_match_circuit()
@pytest.mark.parametrize("cirq_qubit_type", ["LineQubit", "GridQubit", "NamedQubit"])
def test_conversions(cirq_qubit_type: str) -> None:
circ = get_match_circuit(cirq_qubit_type=cirq_qubit_type)
coms = cirq_to_tk(circ)

cirq_false = tk_to_cirq(coms, copy_all_qubits=False)
Expand All @@ -85,9 +94,27 @@ def test_device() -> None:
assert str(arc) == "<tket::Architecture, nodes=54>"


def test_parallel_ops() -> None:
q0, q1, q2 = [cirq.LineQubit(i) for i in range(3)]
@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
if cirq_qubit_type == "GridQubit":
q0, q1, q2 = GridQubit.rect(rows=1, cols=3) # type: ignore
if cirq_qubit_type == "NamedQubit":
q0, q1, q2 = NamedQubit.range(3, prefix="cirq") # type: ignore
circ = cirq.Circuit([cirq.ops.ParallelGate(cirq.Y**0.3, 3).on(q0, q1, q2)])
c_tk = cirq_to_tk(circ)
assert c_tk.n_gates_of_type(OpType.Ry) == 3
assert c_tk.n_gates == 3


def test_unsupported_qubit_type() -> None:
qdit = cirq.LineQid(1, dimension=2)
circ = cirq.Circuit(
[
cirq.H(qdit),
]
)
with pytest.raises(NotImplementedError) as error:
cirq_to_tk(circ)
assert "Cannot convert qubits of type" in str(error.value)
Loading

0 comments on commit ec16228

Please sign in to comment.