question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cirq.decompose semantics are confusing

See original GitHub issue

I believe there is a bug in the keep functionality of cirq.decompose. Perhaps I am just using it wrong. Consider the following code.

class MyGate(cirq.TwoQubitGate, cirq.InterchangeableQubitsGate):
    def _decompose_(self, qubits):
        a, b = qubits
        yield cirq.XXPowGate(global_shift=-0.5, exponent=0.5).on(a, b)
        yield cirq.YYPowGate(global_shift=-0.5, exponent=0.5).on(a, b)

    def __repr__(self):
        return 'MyGate'

class MyOtherGate(cirq.TwoQubitGate, cirq.InterchangeableQubitsGate):
    def _decompose_(self, qubits):
        a, b = qubits
        yield (cirq.Z ** 1.4321001256099544).on(a)
        yield cirq.CZ(a, b) ** (-3 / 17)
        yield MyGate().on(a, b)
        yield cirq.YYPowGate(global_shift=-0.5, exponent=0.5).on(a, b)
       
def stop_at_mygate(gate):
    return isinstance(gate, MyGate)

a, b = cirq.LineQubit.range(2)
circuit = cirq.Circuit.from_ops(
    cirq.decompose(MyOtherGate().on(a, b), keep=stop_at_mygate))
print(circuit)

I would have expected this code to decompose things until stopping at “MyGate”. Instead, it assumes that I really want it to decompose everything that isn’t MyGate, including things that cannot be decomposed further. Thus, I get the error message: ValueError: Operation doesn't satisfy the given keep but can't be decomposed: (cirq.Z**1.4321001256099544).on(cirq.LineQubit(0)) This is very counter intuitive at best. But fine, perhaps when I use this keep functionality I need to explicitly tell the decomposer to stop when things don’t have a decompose method. Thus, I tried changing stop_at_mygate in the following way:

def stop_at_mygate(gate):
    return (isinstance(gate, MyGate) or
            not hasattr(gate, '_decompose_'))

But that also did not work. What is going on here?

As a final note, if I do decompose things all the way (I don’t pass the keep argument) then things get compiled into Molmer-Sorensen gates. Who ordered that!? Those gates are only relevant for ion traps. My feeling is that nothing in Cirq should compile to MS gates by default.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
c-poolecommented, May 15, 2019

Also your “keep” method

def stop_at_mygate(gate): return isinstance(gate, MyGate) will only return True if the thing that is passed into it is a Gate. You are passing it GateOperations via your generator. Does the following “keep” function give you behavior you expect?

def stop_at_mygate(thing):  
    if isinstance(thing, GateOperation):  
         return isinstance(thing.gate, MyGate)  
     return isinstance(gate, MyGate)

EDIT: What is your expected output?

1reaction
c-poolecommented, May 14, 2019

An XXPowGate with global shift of -0.5 is by definition a MS gate. The reason an MS gate shows up in your circuit is because you put one in it, not because decompose is attempting to construct a circuit with it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mølmer–Sørensen gate should be disentangled from XX parity ...
I'll create a new MSGate in ion_gates.py which derives from XXPowGate and would show the correct name in repr , str etc. Does...
Read more >
cirq.decompose - Google Quantum AI
Recursively decomposes a value into cirq.Operations meeting a criteria.
Read more >
arXiv:2003.02989v2 [quant-ph] 26 Aug 2021
Minimalism. Cirq provides an extensive set of tools for preparing quantum circuits. TensorFlow provides a very complete machine learning toolkit.
Read more >
QDiff: Differential Testing of Quantum Software Stacks
widely-used QSSes: Qiskit, Cirq, and Pyquil. With six seed quantum algorithms, QDIFF generates 730 variant algorithms through semantics-modifying mutations.
Read more >
Bugs in Quantum Computing Platforms: An Empirical Study
Qiskit [Qis 2021] and Cirq [Developers. 2021], two large-scale projects ... confusing basic data types, such as Python's tuple and list.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found