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

cirq.Simulator doesn't support inverse gates that use sympy arguments for gates that don't define __pow__ #4655

Closed
augustehirth opened this issue Nov 10, 2021 · 1 comment · Fixed by #4656
Assignees
Labels

Comments

@augustehirth
Copy link
Collaborator

augustehirth commented Nov 10, 2021

Simulating the inverse of a gate that uses sympy Symbols as arguments causes a "Simulator doesn't support" Type Error, if the gate inherits its pow magic method from cirq.Gate. Of particular note is the cirq.PhasedFSimGate, which decomposes into the cirq.FSimGate and some cirq.Z gates. The cirq.FSimGate has it's own pow method implemented, and can correctly be inverted and simulated when using sympy arguments. It seems likely that somewhere in the pipeline, the _InverseCompositeGate produced by cirq.Gate's pow is being rejected as an unsupported gate instead of being decomposed, due to it's use of sympy arguments. @tanujkhattar, can you provide more insight on this?

Replication: Create a circuit with an inverse PhasedFSimGate that uses sympy arguments and attempt to simulate it. This issue doesn't occur with FSimGate, which is the primary component of PhasedFSimGate's decomposition.

import cirq
import sympy
simulator = cirq.Simulator()
qubit0, qubit1 = cirq.GridQubit(0,0), cirq.GridQubit(0,1)
theta, phi = sympy.Symbol('theta'), sympy.Symbol('phi')
for gate in [cirq.FSimGate(theta, phi), cirq.PhasedFSimGate(theta, phi)]: 
    for exponent in [1, -1]: 
        circuit = cirq.Circuit()
        circuit.append(pow(gate, exponent).on(qubit0, qubit1))
        results = simulator.simulate(circuit, param_resolver={'theta': 0, 'phi': 0})
        print(results)

Output log

measurements: (no measurements)
output vector: |00⟩
measurements: (no measurements)
output vector: |00⟩
measurements: (no measurements)
output vector: |00⟩
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/cirq/sim/simulator_base.py in _core_iterator(self, circuit, sim_state, all_measurements_are_terminal)
    220                     # Simulate the operation
--> 221                     protocols.act_on(op, sim_state)
    222                 except TypeError:

11 frames
TypeError: Can't simulate operations that don't implement SupportsUnitary, SupportsConsistentApplyUnitary, SupportsMixture or is a measurement: (cirq.Z**sympy.Mul(sympy.Float('0.5', precision=53), sympy.Pow(sympy.pi, sympy.Integer(-1)), sympy.Symbol('phi'))).on(cirq.GridQubit(0, 1))

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/cirq/sim/simulator_base.py in _core_iterator(self, circuit, sim_state, all_measurements_are_terminal)
    221                     protocols.act_on(op, sim_state)
    222                 except TypeError:
--> 223                     raise TypeError(f"{self.__class__.__name__} doesn't support {op!r}")
    224 
    225             step_result = self._create_step_result(sim_state)

TypeError: Simulator doesn't support (cirq.PhasedFSimGate(theta=sympy.Symbol('theta'), zeta=sympy.Symbol('phi'), chi=0.0, gamma=0.0, phi=0.0)**-1).on(cirq.GridQubit(0, 0), cirq.GridQubit(0, 1))

Cirq version
0.14.0.dev20211110190326

@tanujkhattar
Copy link
Collaborator

Sent a PR to fix the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants