Having n qubits, I want to have the unitary described a controlled operation. Say for example you get as input a unitary, an index for a controlled qubit and another for a target.

How would you code this unitary operation?

  • $\begingroup$ What do you mean ‘code’? What sort of output are you after? $\endgroup$
    – DaftWullie
    Aug 4, 2018 at 11:35
  • $\begingroup$ Sorry. I meant like a function taking those parameters and outputting the matrix $\endgroup$
    – cnada
    Aug 4, 2018 at 11:40
  • $\begingroup$ So you have $n$ qubits and want to do a $2$-qubit gate which is a controlled unitary? $\endgroup$ Aug 4, 2018 at 11:41
  • $\begingroup$ Yes. But getting the unitary on all n qubits. Say like you do a simple CNOT or CZ and you wanna get the 2^n*2^n matrix representing it. $\endgroup$
    – cnada
    Aug 4, 2018 at 11:44

2 Answers 2


Here’s some pseudo code, where id(n) creates an $2^n\times 2^n$ identity matrix, and tensor(A,B,...) returns $A\otimes B\otimes\ldots$.

def cU(ctrl,targ,U,size):
    '''implement controlled-U with:
          control qubit ctrl,
          target qubit targ,
          within a set of size qubits'''
#check input ranges
    assert 1<=ctrl<=size
    assert 1<=targ<=size
    assert ctrl<>targ
    assert ctrl,targ,size ∊ ℤ
#ensure U is a 2x2 unitary
    assert U∊ℂ2x2
    assert U.U=id(2)
#the actual code
    if ctrl<targ:
        return id(size)+tensor(id(ctrl-1),id(1)-Z,id(targ-1-ctrl),U-id(1),id(size-targ))/2
        return id(size)+tensor(id(targ-1),U-id(1),id(ctrl-1-targ),id(1)-Z,id(size-ctrl))/2

However, remember that usually you're trying to calculate the action of a unitary on some state vector. It will be far more memory efficient to calculate that application directly, rather than first calculating the unitary matrix and applying it to the state vector.

To understand where this formula came from, think about the two-qubit version, where the first qubit is the control qubit. You'd normally write the unitary as $$ |0\rangle\langle 0|\otimes\mathbb{I}+|1\rangle\langle 1|\otimes U. $$ Let's rewrite this as $$ (\mathbb{I}-|1\rangle\langle 1|)\otimes\mathbb{I}+|1\rangle\langle 1|\otimes U=\mathbb{I}\otimes\mathbb{I}+|1\rangle\langle 1|\otimes (U-\mathbb{I}). $$ It can be easier to write things in terms of Pauli matrices, so $$ |1\rangle\langle 1|=(\mathbb{I}-Z)/2. $$ To get the same unitary on a different number of qubits, you just need to pad with identity matrices everywhere.

  • $\begingroup$ can you explain the formula you use in your code? Can I ask where did you get it from? $\endgroup$
    – cnada
    Aug 5, 2018 at 11:24
  • $\begingroup$ @cnada Does that explain? $\endgroup$
    – DaftWullie
    Aug 5, 2018 at 18:01
  • $\begingroup$ Yes better thanks. But you can just pass the matrix $ |1\rangle\langle 1| $ directly without replacing it (I think it is not needed for the prupose). Also I guess you start the index of your qubits from 1 right (I see you sometimes substract -1 that is why I ask)? $\endgroup$
    – cnada
    Aug 5, 2018 at 20:29
  • 1
    $\begingroup$ Yes, you don’t have to make that replacement. There are many different ways of writing these things, and I chose one. Yes, index starts from 1. So if you want an operator to appear on qubit n, you need n-1 identity operators before it. $\endgroup$
    – DaftWullie
    Aug 5, 2018 at 20:43
  • $\begingroup$ Not the OP, but thank you so much! You were the only one I could find providing an explicit construction. This works perfectly! $\endgroup$
    – LPenguin
    Dec 16, 2020 at 2:02

I would solve it like that: suppose you have CNOT gate; it's unitary matrix can be written as a sum of tensor products $$ \begin{pmatrix}1 & 0 \\ 0 & 0 \end{pmatrix}\otimes \begin{pmatrix}1 & 0 \\ 0 & 1 \end{pmatrix}+ \begin{pmatrix}0 & 0 \\ 0 & 1 \end{pmatrix}\otimes \begin{pmatrix}0 & 1 \\ 1 & 0 \end{pmatrix} $$ (this is another way to say: if first qubit is 0, the second qubit is unchanged; if first qubit is 1, the second qubit is swapped).

Now if we have more qubits, we need to insert additional identity matrices $$ \begin{pmatrix}1 & 0 \\ 0 & 1 \end{pmatrix} $$ into the tensor products from the left, from the right, or into the middle, depending on the indexes of control and target CNOT qubits.

PS: I also assumed that the index of control qubit is less than the index of target qubit; if this is not the case swap the terms in the tensor products in the first equation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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