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$
3
$\begingroup$

In 0.4.0 (current release), you make controlled versions of arbitrary gates by wrapping them with cirq.ControlledGate:

controlled_x = cirq.ControlledGate(cirq.X)
return controlled_x.on(control_qubit, target_qubit)

Controlled gates support most convenience operations, such as asking for the unitary matrix with cirq.unitary(...) or raising to a power. (There are also several built-in controlled gates for specific gates, such as cirq.CNOT, cirq.CCZ, cirq.CSWAP, etc.)

In the next release we'll also support controlling gates after they've been applied to qubits, and will have added a few convenience methods. So in 0.5.0 you'll be able to do this:

return cirq.X(target_qubit).controlled_by(control_qubit)
$\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.