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.

Make ControlledGate work with gates that only provide a decomposition

See original GitHub issue

The following should work:

import cirq


class G(cirq.TwoQubitGate):
    def _decompose_(self, qubits):
        a, b = qubits
        yield cirq.X(a)**0.5
        yield cirq.H(b)
        yield cirq.CZ(a, b)


cg = cirq.ControlledGate(G())
x, y, z = cirq.LineQubit.range(3)
c = cirq.Circuit.from_ops(cg(x, y, z))
print(c.to_unitary_matrix())

but currently it raises an exception:

Operation without a known matrix or decomposition: cirq.ControlledGate(sub_gate=[...G...].on(cirq.LineQubit(0), cirq.LineQubit(1), cirq.LineQubit(2))

because ControlledGate doesn’t have a _decompose_ method. This issue is to add that method.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
vtomolecommented, Jan 17, 2019

@balopat Go for it 😃

0reactions
Strilanccommented, Jan 18, 2019

This ended up being more complicated than I expected, because it requires defining a controlled operation and understanding how the decomposition machinery works in detail.

    def _decompose_(self, qubits):
        control_qubit = qubits[0]
        result = protocols.decompose_once_with_qubits(self.sub_gate,
                                                      qubits[1:],
                                                      NotImplemented)
        if result is NotImplemented:
            return NotImplemented
        for op in result:
            yield ControlledOperation(control_qubit, op)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to decompose a multi-target controlled gate?
Firstly one needs to decompose the given U operator into gates, then for each gate construct the controlled version of it.
Read more >
Quantum Gate Decomposition Algorithms - OSTI.gov
Two-qubit gates operate through the relatively weak coupling of two qubits, making such gates more difficult to implement. Therefore, efficient de- compositions ...
Read more >
Gates, States, and Circuits - Gavin E. Crooks
The geometric representation of 1-qubit states by the Bloch sphere only works because of a mathematical accident that doesn't generalize.
Read more >
Method for reducing a uniformly controlled gate into nearest ...
A collection of quantum gates is called universal if it can be used to construct any n-qubit gate. In... | Decomposition, Quantum and...
Read more >
cirq.SupportsDecompose - Google Quantum AI
All decomposition methods should ultimately terminate on basic 1-qubit and 2-qubit gates included by default in Cirq. Cirq does not make any ...
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