3
$\begingroup$

My question is very simple. I know there is a method to add a control to previously defined gates. I saw in the API of cirq that there is a command Controlled gate (here). I'm not understanding though how would this work. Suppose for example I want to add a control to an XPowGate from cirq. How would I write this in a program and how would I define the control qubit and the qubit over which I want to act?

$\endgroup$
5
$\begingroup$

In Cirq v0.5.0 and later you can use the controlled_by method on any Operation:

op = cirq.X(target_qubit).controlled_by(control_qubit)

You can also use controlled_by on any Gate, i.e. before specifying the target qubits:

op = cirq.X.controlled_by(control_qubit).on(target_qubit)

And you can also make a controlled version of the gate with an initially unspecified control qubit:

op = cirq.ControlledGate(cirq.X).on(control_qubit, target_qubit)

There are also built-in controlled operations such as cirq.CNOT, cirq.CZ, and cirq.CSWAP. The built-in operations are generally preferable because they have hand-tuned gate decompositions and simulation methods. The general controlled_by has to fall back to more general methods.

Note that the controlled versions of gates support things like raising to a power. The following three expressions produce equivalent operations:

(cirq.X(q)**0.5).controlled_by(c)

(cirq.X**0.5).on(q).controlled_by(c)

cirq.X(q).controlled_by(c)**0.5
$\endgroup$
  • $\begingroup$ Thank you! if I have to a two-qubit gate and want to add a control, then I would have to write: controlled_x = cirq.ControlledGate(cirq.TwoQubitGate) return controlled_x.on(control_qubit, target_qubit1, target_qubit2) right? $\endgroup$ – Pam Feb 19 at 6:31
  • $\begingroup$ @Pam That's right. $\endgroup$ – Craig Gidney Feb 19 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.