Skip to content

Make ControlledGate work with gates that only provide a decomposition #1284

@Strilanc

Description

@Strilanc

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions