-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Pasqal to use transformers (#5377)
- Add PasqalGateset that encapsulates gatesets used by the PasqalDevice classes. - Deprecate PasqalConverter and use cirq.convert_to_target_gateset instead. Fixes: #5130
- Loading branch information
1 parent
352a976
commit 289f9ac
Showing
7 changed files
with
189 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"cirq_type": "PasqalGateset", | ||
"include_additional_controlled_ops": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cirq_pasqal.PasqalGateset(include_additional_controlled_ops=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2022 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. | ||
# | ||
from typing import Any, Dict | ||
|
||
|
||
import cirq | ||
|
||
|
||
class PasqalGateset(cirq.neutral_atoms.NeutralAtomGateset): | ||
"""A Compilation target intended for Pasqal neutral atom devices. | ||
This gateset supports single qubit gates that can be used | ||
in a parallel fashion as well as CZ. | ||
This gateset can optionally include CNOT, CCNOT (TOFFOLI) gates, and | ||
CCZ as well. | ||
Args: | ||
include_additional_controlled_ops: Whether to include CCZ, CCNOT, and CNOT | ||
gates (defaults to True). | ||
""" | ||
|
||
def __init__(self, include_additional_controlled_ops: bool = True): | ||
gate_families = [ | ||
cirq.ParallelGateFamily(cirq.H), | ||
cirq.ParallelGateFamily(cirq.PhasedXPowGate), | ||
cirq.ParallelGateFamily(cirq.XPowGate), | ||
cirq.ParallelGateFamily(cirq.YPowGate), | ||
cirq.ParallelGateFamily(cirq.ZPowGate), | ||
cirq.AnyIntegerPowerGateFamily(cirq.CZPowGate), | ||
cirq.IdentityGate, | ||
cirq.MeasurementGate, | ||
] | ||
self.include_additional_controlled_ops = include_additional_controlled_ops | ||
if self.include_additional_controlled_ops: | ||
gate_families.append(cirq.AnyIntegerPowerGateFamily(cirq.CNotPowGate)) | ||
gate_families.append(cirq.AnyIntegerPowerGateFamily(cirq.CCNotPowGate)) | ||
gate_families.append(cirq.AnyIntegerPowerGateFamily(cirq.CCZPowGate)) | ||
|
||
# Call cirq.Gateset __init__ which is our grand-father inherited class | ||
# pylint doesn't like this so disable checks on this. | ||
# pylint: disable=bad-super-call | ||
super(cirq.neutral_atoms.NeutralAtomGateset, self).__init__( | ||
*gate_families, unroll_circuit_op=False | ||
) | ||
|
||
def __repr__(self): | ||
return ( | ||
f'cirq_pasqal.PasqalGateset(include_additional_controlled_ops=' | ||
f'{self.include_additional_controlled_ops})' | ||
) | ||
|
||
@classmethod | ||
def _from_json_dict_(cls, include_additional_controlled_ops, **kwargs): | ||
return cls(include_additional_controlled_ops=include_additional_controlled_ops) | ||
|
||
def _json_dict_(self) -> Dict[str, Any]: | ||
return cirq.protocols.obj_to_dict_helper(self, ['include_additional_controlled_ops']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2022 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 cirq | ||
import cirq_pasqal | ||
|
||
Q, Q2, Q3 = cirq.LineQubit.range(3) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"op,expected", | ||
[ | ||
(cirq.H(Q), True), | ||
(cirq.HPowGate(exponent=0.5)(Q), False), | ||
(cirq.PhasedXPowGate(exponent=0.25, phase_exponent=0.125)(Q), True), | ||
(cirq.XPowGate(exponent=0.5)(Q), True), | ||
(cirq.YPowGate(exponent=0.25)(Q), True), | ||
(cirq.ZPowGate(exponent=0.125)(Q), True), | ||
(cirq.CZPowGate(exponent=0.5)(Q, Q2), False), | ||
(cirq.CZ(Q, Q2), True), | ||
(cirq.CNOT(Q, Q2), True), | ||
(cirq.SWAP(Q, Q2), False), | ||
(cirq.ISWAP(Q, Q2), False), | ||
(cirq.CCNOT(Q, Q2, Q3), True), | ||
(cirq.CCZ(Q, Q2, Q3), True), | ||
(cirq.ParallelGate(cirq.X, num_copies=3)(Q, Q2, Q3), True), | ||
(cirq.ParallelGate(cirq.Y, num_copies=3)(Q, Q2, Q3), True), | ||
(cirq.ParallelGate(cirq.Z, num_copies=3)(Q, Q2, Q3), True), | ||
(cirq.X(Q).controlled_by(Q2, Q3), True), | ||
(cirq.Z(Q).controlled_by(Q2, Q3), True), | ||
(cirq.ZPowGate(exponent=0.5)(Q).controlled_by(Q2, Q3), False), | ||
], | ||
) | ||
def test_gateset(op: cirq.Operation, expected: bool): | ||
gs = cirq_pasqal.PasqalGateset() | ||
assert gs.validate(op) == expected | ||
assert gs.validate(cirq.Circuit(op)) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"op,expected", | ||
[ | ||
(cirq.H(Q), True), | ||
(cirq.HPowGate(exponent=0.5)(Q), False), | ||
(cirq.PhasedXPowGate(exponent=0.25, phase_exponent=0.125)(Q), True), | ||
(cirq.ParallelGate(cirq.X, num_copies=3)(Q, Q2, Q3), True), | ||
(cirq.CZPowGate(exponent=0.5)(Q, Q2), False), | ||
(cirq.CZ(Q, Q2), True), | ||
(cirq.CNOT(Q, Q2), False), | ||
(cirq.CCNOT(Q, Q2, Q3), False), | ||
(cirq.CCZ(Q, Q2, Q3), False), | ||
(cirq.Z(Q).controlled_by(Q2), True), | ||
(cirq.X(Q).controlled_by(Q2, Q3), False), | ||
(cirq.Z(Q).controlled_by(Q2, Q3), False), | ||
(cirq.ZPowGate(exponent=0.5)(Q).controlled_by(Q2, Q3), False), | ||
], | ||
) | ||
def test_control_gates_not_included(op: cirq.Operation, expected: bool): | ||
gs = cirq_pasqal.PasqalGateset(include_additional_controlled_ops=False) | ||
assert gs.validate(op) == expected | ||
assert gs.validate(cirq.Circuit(op)) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"op", | ||
[ | ||
cirq.X(Q), | ||
cirq.SWAP(Q, Q2), | ||
cirq.ISWAP(Q, Q2), | ||
cirq.CCNOT(Q, Q2, Q3), | ||
cirq.CCZ(Q, Q2, Q3), | ||
cirq.ParallelGate(cirq.X, num_copies=3)(Q, Q2, Q3), | ||
cirq.SWAP(Q, Q2).controlled_by(Q3), | ||
], | ||
) | ||
def test_decomposition(op: cirq.Operation): | ||
circuit = cirq.Circuit(op) | ||
gs = cirq_pasqal.PasqalGateset() | ||
gs2 = cirq_pasqal.PasqalGateset(include_additional_controlled_ops=False) | ||
for gateset in [gs, gs2]: | ||
decomposed_circuit = cirq.optimize_for_target_gateset(circuit, gateset=gateset) | ||
for new_op in decomposed_circuit.all_operations(): | ||
assert gs.validate(new_op) | ||
|
||
|
||
def test_repr(): | ||
cirq.testing.assert_equivalent_repr( | ||
cirq_pasqal.PasqalGateset(), setup_code='import cirq_pasqal' | ||
) | ||
cirq.testing.assert_equivalent_repr( | ||
cirq_pasqal.PasqalGateset(include_additional_controlled_ops=False), | ||
setup_code='import cirq_pasqal', | ||
) |