![]() |
GateFamily useful to convert/accept cirq.FSimGate
and other related gate types.
cirq_google.ops.FSimGateFamily(
*,
gates_to_accept: Sequence[Union[Type[POSSIBLE_FSIM_GATES], POSSIBLE_FSIM_GATES]] = (),
gate_types_to_check: Sequence[Type[POSSIBLE_FSIM_GATES]] = (),
allow_symbols: bool = False,
atol=DEFAULT_ATOL
)
This gate family is useful to work with any of the different representations of cirq.FSimGate
and its related types present in Cirq. Specifically, the gate family can be used to convert or
accept (if possible) compatible instances of any of the following POSSIBLE_FSIM_GATES
types:
cirq.FSimGate
,cirq.PhasedFSimGate
cirq.ISwapPowGate
,cirq.PhasedISwapPowGate
cirq.CZPowGate
cirq.IdentityGate
The gate family also allows an option to accept parameterized gates, assuming that the correct parameters would eventually be filled.
For example,
- To convert to/from any of the non-parameterized
POSSIBLE_FSIM_GATES
types:
>>> gf = cirq_google.FSimGateFamily()
>>> assert gf.convert(cirq.FSimGate(np.pi/4, 0), cirq.ISwapPowGate) == cirq.SQRT_ISWAP_INV
>>> assert gf.convert(cirq.PhasedFSimGate(0, 0, 0, 0, np.pi), cirq.CZPowGate) == cirq.CZ
>>> assert gf.convert(cirq.FSimGate(-np.pi/2, sympy.Symbol("t")), cirq.ISwapPowGate) is None
- To convert to/from any of the parameterized
POSSIBLE_FSIM_GATES
types (assuming correct value of the parameter would be filled in during paramter resolution):
>>> gf = cirq_google.FSimGateFamily(allow_symbols = True)
>>> theta, phi = sympy.Symbol("theta"), sympy.Symbol("phi")
>>> assert gf.convert(cirq.FSimGate(-np.pi/4, phi), cirq.ISwapPowGate) == cirq.SQRT_ISWAP
>>> assert gf.convert(cirq.PhasedFSimGate(theta, 0, 0, 0, np.pi), cirq.CZPowGate) == cirq.CZ
>>> assert gf.convert(cirq.FSimGate(theta, phi), cirq.IdentityGate) == cirq.IdentityGate(2)
- To accept instances of
gate_types_to_check
based on type / value equality check against gates ingates_to_accept
, possibly accepting parameterized instances (assuming correct parameter value would be filled in during parameter resolution) based onallow_symbols
:
>>> gf = cirq_google.FSimGateFamily(
>>> gates_to_accept=[cirq.SQRT_ISWAP, cirq.CZPowGate, cirq_google.SYC],
>>> gate_types_to_check=[cirq.FSimGate],
>>> allow_symbols=True,
>>> )
>>> theta, phi = sympy.Symbol("theta"), sympy.Symbol("phi")
>>> assert cirq.FSimGate(theta, phi) in gf # Assumes correct theta/phi will be substituted.
>>> assert cirq_google.SYC in gf # SYC
>>> assert cirq.FSimGate(0, np.pi / 2) in gf # CZPowGate
>>> assert cirq.FSimGate(-np.pi / 4, phi) in gf # SQRT_ISWAP
>>> assert cirq.FSimGate(-np.pi / 8, phi) not in gf # No value of `phi` would make it equal to
>>> # any gate/gate type in `gates_to_accept`.
>>> assert cirq.CZ ** 0.25 not in gf # CZPowGate not in gate_types_to_check
>>> assert cirq.SQRT_ISWAP not in gf # ISwapPowGate not in gate_types_to_check
Args | |
---|---|
gates_to_accept
|
List of gate types or instances to be accepted. All elements
should be either types from POSSIBLE_FSIM_GATES or non-parameterized
instances of those types.
|
gate_types_to_check
|
List of POSSIBLE_FSIM_GATES types whose instances should be
considered when trying to match against gates present in gates_to_accept .
Defaults to all POSSIBLE_FSIM_GATES if left unspecified.
|
allow_symbols
|
If True, both the gate conversion logic and containment predicate allow parameterized gate instances and return a converted gate / accept input gate if there exists any value of the unknown parameters which can result in a valid outcome. |
atol
|
Absolute tolerance for difference floating point comparisons. |
Raises | |
---|---|
ValueError
|
If any gate in gates_to_accept is not a non-parameterized instance of /
or a gate type from POSSIBLE_FSIM_GATES .
|
ValueError
|
If any gate type in gate_types_to_check is not one of
POSSIBLE_FSIM_GATES .
|
Attributes | |
---|---|
description
|
|
gate
|
|
name
|
Methods
convert
convert(
gate: cirq.Gate, target_gate_type: Type[T]
) -> Optional[T]
Converts, if possible, the given gate
to an equivalent instance of target_gate_type
.
This method can be used for converting instances of POSSIBLE_FSIM_GATES
to other
equivalent types from the same group. For example, you can convert a sqrt iswap gate
to an equivalent fsim gate by calling:
gf = cirq_google.FSimGateFamily()
assert gf.convert(cirq.SQRT_ISWAP, cirq.FSimGate) == cirq.FSimGate(-np.pi/4, 0)
The method can also be used for converting parameterized gate instances, by setting
allow_symbols=True
in the gate family constructor. Note that, conversion of
paramaterized gate instances tries to be lenient and assumes that the correct
parameters would eventually be filled during parameter resolution. This can also result
in dropping extra parameters during type conversion, assuming the dropped parameters
would be supplied the correct values. For example:
gf = cirq_google.FSimGateFamily(allow_symbols = True)
theta, phi = sympy.Symbol("theta"), sympy.Symbol("phi")
assert gf.convert(cirq.FSimGate(-np.pi/4, phi), cirq.ISwapPowGate) == cirq.SQRT_ISWAP
assert gf.convert(cirq.FSimGate(theta, np.pi/4), cirq.ISwapPowGate) is None
Args | |
---|---|
gate
|
cirq.Gate instance to convert.
|
target_gate_type
|
One of POSSIBLE_FSIM_GATES types to which the given gate should be
converted to.
|
Returns | |
---|---|
The converted gate instances if the converion is possible, else None. |
Raises | |
---|---|
ValueError
|
If target_gate_type is not one of POSSIBLE_FSIM_GATES .
|
__contains__
__contains__(
item: Union[raw_types.Gate, raw_types.Operation]
) -> bool
__eq__
__eq__(
other: _SupportsValueEquality
) -> bool
__ne__
__ne__(
other: _SupportsValueEquality
) -> bool