# Error "too many subscripts in einsum", UnitaryGate()

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:  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?

• 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?
– Lena
Mar 30, 2021 at 15:50
• 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? Mar 30, 2021 at 16:28

_list = []

• 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. Mar 31, 2021 at 8:50