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

Deprecate Gateset.accept_global_phase_op #5239

Merged
merged 27 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a6e6d78
Deprecate Gateset.accept_global_phase_op
daxfohl Apr 9, 2022
679a4b2
Change gateset accept_global_phase_op default to False.
daxfohl Apr 9, 2022
4d953cc
Fix up uses of Gateset for the breaking GlobalPhaseGate change
daxfohl Apr 9, 2022
4d8bff0
Fix allow fallback to checking for GlobalPhaseGate in gateset
daxfohl Apr 9, 2022
5489cbb
Fix json
daxfohl Apr 9, 2022
eb0527d
format
daxfohl Apr 9, 2022
c463a0c
Physically add a GlobalPhaseGate if allow_global_phase is True, rewor…
daxfohl Apr 9, 2022
a5245e9
fix doc
daxfohl Apr 9, 2022
a7e5c02
Update docs, add inward files
daxfohl Apr 9, 2022
7df5e95
Add json parsing deprecation test
daxfohl Apr 9, 2022
144b897
Add assertions to new test
daxfohl Apr 9, 2022
621e00c
minor refactor
daxfohl Apr 10, 2022
5deea64
Force phase gate removal if allow=False'
daxfohl Apr 13, 2022
71015ae
add global phase to gatesets
daxfohl Apr 13, 2022
68d9951
fix tests in google
daxfohl Apr 13, 2022
1a1ba13
fix tests in google
daxfohl Apr 13, 2022
57a1745
Add warning when default `accept_global_phase_op`
daxfohl Apr 14, 2022
53fd095
Fix pasqal test
daxfohl Apr 14, 2022
ff2b152
Merge branch 'master' into dep-accepts-global-phase
daxfohl Apr 14, 2022
5aca223
format
daxfohl Apr 14, 2022
22d1ed8
Merge branch 'master' into dep-accepts-global-phase
daxfohl Apr 14, 2022
f7c12da
inward json parser
daxfohl Apr 15, 2022
7d45446
coverage
daxfohl Apr 15, 2022
01fdb0b
Fix warnings
daxfohl Apr 17, 2022
a648a61
format
daxfohl Apr 17, 2022
ac31654
Merge branch 'master' into dep-accepts-global-phase
daxfohl Apr 17, 2022
4d152e4
Merge branch 'master' into dep-accepts-global-phase
CirqBot Apr 18, 2022
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
Next Next commit
Deprecate Gateset.accept_global_phase_op
  • Loading branch information
daxfohl committed Apr 9, 2022
commit a6e6d7802866bdef0d9dd80c5f48386b988ec3b1
25 changes: 22 additions & 3 deletions cirq-core/cirq/ops/gateset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from typing import Any, Callable, cast, Dict, FrozenSet, List, Optional, Type, TYPE_CHECKING, Union
from cirq.ops import global_phase_op, op_tree, raw_types
from cirq import protocols, value
from cirq import _compat, protocols, value

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -201,6 +201,12 @@ class Gateset:
validation purposes.
"""

@_compat.deprecated_parameter(
deadline='v0.16',
fix='Add a global phase gate to the Gateset',
daxfohl marked this conversation as resolved.
Show resolved Hide resolved
parameter_desc='ignore_global_phase',
match=lambda args, kwargs: 'accept_global_phase_op' in kwargs,
)
def __init__(
self,
*gates: Union[Type[raw_types.Gate], raw_types.Gate, GateFamily],
Expand All @@ -225,7 +231,7 @@ def __init__(
name: (Optional) Name for the Gateset. Useful for description.
unroll_circuit_op: If True, `cirq.CircuitOperation` is recursively
validated by validating the underlying `cirq.Circuit`.
accept_global_phase_op: If True, `cirq.GlobalPhaseOperation` is accepted.
accept_global_phase_op: If True, `cirq.GlobalPhaseGate` is accepted.
"""
self._name = name
self._unroll_circuit_op = unroll_circuit_op
Expand Down Expand Up @@ -253,6 +259,12 @@ def name(self) -> Optional[str]:
def gates(self) -> FrozenSet[GateFamily]:
return self._gates

@_compat.deprecated_parameter(
deadline='v0.16',
fix='Add a global phase gate to the Gateset',
parameter_desc='ignore_global_phase',
match=lambda args, kwargs: 'accept_global_phase_op' in kwargs,
)
def with_params(
self,
*,
Expand Down Expand Up @@ -287,11 +299,18 @@ def val_if_none(var: Any, val: Any) -> Any:
and accept_global_phase_op == self._accept_global_phase_op
):
return self
accept_global_phase = cast(bool, accept_global_phase_op)
if not accept_global_phase:
return Gateset(
*self.gates,
name=name,
unroll_circuit_op=cast(bool, unroll_circuit_op),
accept_global_phase_op=False,
)
return Gateset(
*self.gates,
name=name,
unroll_circuit_op=cast(bool, unroll_circuit_op),
accept_global_phase_op=cast(bool, accept_global_phase_op),
)

def __contains__(self, item: Union[raw_types.Gate, raw_types.Operation]) -> bool:
Expand Down
60 changes: 34 additions & 26 deletions cirq-core/cirq/ops/gateset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ def test_gateset_init():


def test_gateset_repr_and_str():
cirq.testing.assert_equivalent_repr(gateset)
with cirq.testing.assert_deprecated('global phase', deadline='v0.16', count=3):
cirq.testing.assert_equivalent_repr(gateset)
assert gateset.name in str(gateset)
for gate_family in gateset.gates:
assert str(gate_family) in str(gateset)
Expand Down Expand Up @@ -263,23 +264,27 @@ def assert_validate_and_contains_consistent(gateset, op_tree, result):
assert gateset.validate(item) is result

op_tree = [*get_ops(use_circuit_op, use_global_phase)]
assert_validate_and_contains_consistent(
gateset.with_params(
unroll_circuit_op=use_circuit_op,
accept_global_phase_op=use_global_phase,
),
op_tree,
True,
)
if use_circuit_op or use_global_phase:
with cirq.testing.assert_deprecated(
'global phase', deadline='v0.16', count=1 if use_global_phase else 2
):
assert_validate_and_contains_consistent(
gateset.with_params(
unroll_circuit_op=False,
accept_global_phase_op=False,
unroll_circuit_op=use_circuit_op,
accept_global_phase_op=use_global_phase,
),
op_tree,
False,
True,
)
if use_circuit_op or use_global_phase:
with cirq.testing.assert_deprecated('global phase', deadline='v0.16', count=2):
assert_validate_and_contains_consistent(
gateset.with_params(
unroll_circuit_op=False,
accept_global_phase_op=False,
),
op_tree,
False,
)


def test_gateset_validate_circuit_op_negative_reps():
Expand All @@ -291,17 +296,19 @@ def test_gateset_validate_circuit_op_negative_reps():

def test_with_params():
assert gateset.with_params() is gateset
assert (
gateset.with_params(
name=gateset.name,
unroll_circuit_op=gateset._unroll_circuit_op,
accept_global_phase_op=gateset._accept_global_phase_op,
with cirq.testing.assert_deprecated('global phase', deadline='v0.16', count=1):
assert (
gateset.with_params(
name=gateset.name,
unroll_circuit_op=gateset._unroll_circuit_op,
accept_global_phase_op=gateset._accept_global_phase_op,
)
is gateset
)
with cirq.testing.assert_deprecated('global phase', deadline='v0.16', count=2):
gateset_with_params = gateset.with_params(
name='new name', unroll_circuit_op=False, accept_global_phase_op=False
)
is gateset
)
gateset_with_params = gateset.with_params(
name='new name', unroll_circuit_op=False, accept_global_phase_op=False
)
assert gateset_with_params.name == 'new name'
assert gateset_with_params._unroll_circuit_op is False
assert gateset_with_params._accept_global_phase_op is False
Expand All @@ -313,9 +320,10 @@ def test_gateset_eq():
eq.add_equality_group(cirq.Gateset(CustomX**3))
eq.add_equality_group(cirq.Gateset(CustomX, name='Custom Gateset'))
eq.add_equality_group(cirq.Gateset(CustomX, name='Custom Gateset', unroll_circuit_op=False))
eq.add_equality_group(
cirq.Gateset(CustomX, name='Custom Gateset', accept_global_phase_op=False)
)
with cirq.testing.assert_deprecated('global phase', deadline='v0.16', count=1):
eq.add_equality_group(
cirq.Gateset(CustomX, name='Custom Gateset', accept_global_phase_op=False)
)
eq.add_equality_group(
cirq.Gateset(
cirq.GateFamily(CustomX, name='custom_name', description='custom_description'),
Expand Down