0
$\begingroup$

I'd like to build a multi qubit controlled gate in the following way.
The circuit has $n+1$ qubits, the first qubit is the control qubit, and the operation on the rest $n$ qubits is the tensor product of $n$ phase rotations $R^{\otimes n}$.

from qiskit import *
from qiskit.extensions import *
import qiskit.extensions.unitary
import cmath as cm
import numpy as np
import math as m
from qiskit.aqua.utils import tensorproduct


n=10
c = QuantumRegister(1, "c")
q = QuantumRegister(n, "q")
cl = ClassicalRegister(2, "cl")
circ = QuantumCircuit( c, q, cl)


phase=cm.exp(cm.pi*complex(0,1)*(1/4))
matrixR=np.array([[phase,0],[0,1]])
matrixTe= matrixR
for _ in range(n-1):
    matrixTe=tensorproduct(matrixTe,matrixR)
matrixOp=matrixTe

gateCR=UnitaryGate(matrixOp).control(num_ctrl_qubits=1)

qubits=[m for m in reversed(q)]
circ.append(gateCR, qubits) 

For $n\geq10$, I get the following error message:
enter image description here
enter image description here

I understand the problem lies in the size of the matrix when it has phase in it. I tried to build a gate for $n=10$ when the operation has no phase, and it worked without any problem!

My question is, is there any workaround to fix this problem? What does the comment hack to correct global phase; should fix to prevent need for correction here in the source code mean?

$\endgroup$
2
  • $\begingroup$ Hi, just a question : do you plan on changing the phase you use or not? If not, how about instead use this as an operator, then do tensor with the Operator, and control? Here is the Operator : from qiskit.extensions import XGate, TGate \\ from qiskit.quantum_info.operators import Operator \\ X = Operator(XGate()) \\ T = Operator(TGate()) \\ Op = X.compose(T.compose(X)) \\ Also, why not just create a control gate of 2 qubits with one as control and one as target for your matrixR and then just change the target to do all the qubits with the same control? $\endgroup$
    – Lena
    Mar 30 at 15:50
  • $\begingroup$ Yes, the phase changes. To the second point, I'm not splitting the gate because $C(R^ {\otimes n}) \neq (CR)^{\otimes n}$ and $C(R^ {\otimes n}) \neq (CR)^n$. In both cases the matrices in the left and right side of the inequality have different sizes, or I'm missing something? $\endgroup$
    – 22_J
    Mar 30 at 16:28
0
$\begingroup$

You can use Qiskit's MatrixOp, and TensoredOp classes:

_list = []
for _ in range(n):
    _list.append(MatrixOp(matrixR))

tensored_op = TensoredOp(_list)
gateCR=tensored_op.to_circuit().to_gate().control(num_ctrl_qubits = 1)
$\endgroup$
2
  • $\begingroup$ Thanks! I mean, it works but when it comes to the execution on a simulator backend, this method is super slow! It took 4 min. for n=10 to run and my laptop's fan was complaining very loud. XD $\endgroup$
    – 22_J
    Mar 31 at 8:47
  • $\begingroup$ I also compared your method with mine for n=8 to see which one runs faster on the backend. Still, this method is way way slower than mine but at least this one let me to work with bigger ns. $\endgroup$
    – 22_J
    Mar 31 at 8:50

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.