# How fast is qiskit to_gate() method, which is used to convert circuit to gate?

I have prepared a quantum circuit, which is using 13 qubits. Now, I am converting this circuit to gate but it is taking around avg. 35s for conversion. It would have have been fine, if I require only one such converison. But, I need to convert 30 such circuits. Why it is taking such amount of time? Is having 13 qubits have something to do with it? My code:


import networkx as nx
import math
i_zero=[1,0]
i_one=[0,1]
#-----------Initiating the Graph---------------#
G=nx.karate_club_graph()
a=nx.to_numpy_array(G)
c_nodes= len(list(G.nodes))
c_edges=len(G.edges())
#-----------Making the Edge List---------------#
edge_list_tmp=list(G.edges())
edge_list=[]
for i in range(0,c_edges):
v_tmp=edge_list_tmp[i]
li_tmp=[edge_list_tmp[i][0],edge_list_tmp[i][1],False,'coin_state']
edge_list.append(li_tmp)
#-----------Calculating the Qubits Required---------------#
c_pq=math.ceil(math.log2(c_nodes))+1
c_aq=c_pq-1
li_temp=sorted(G.degree, key=lambda x: x[1], reverse=True)
h_deg=li_temp[0][1]
c_cq=math.ceil(math.log2(h_deg))
#----------Making position Transition circuit-------------#
qc_l3=[]
rep_string="{0:0"+str(c_pq-1)+"b}"
for i in range(0,len(edge_list)):
v1=rep_string.format(edge_list[i][0])
v2=rep_string.format(edge_list[i][1])
name=" Transition: "+str(edge_list[i][0])+'->'+str(edge_list[i][1])
qc_temp=QuantumCircuit(c_pq,name=name)
for i in range(0,len(v1)):
if(v1[i]!=v2[i]):
qc_temp.x(c_pq-i-2)
qc_temp.x(6)
qc_l3.append(qc_temp)
#-----------Assigning coin states to each edge------------#
coin_edge={}
curr_v=edge_list[0][0]
rep_string="{0:0"+str(c_cq)+"b}"
for i in range(0,len(edge_list)):
for j in range(0,int(math.pow(2,c_cq))-1):
if not(edge_list[i][2]):
if (edge_list[i][0],rep_string.format(j)) not in coin_edge:
edge_list[i][2]=True
edge_list[i][3]=rep_string.format(j)
coin_edge[(edge_list[i][0],rep_string.format(j))]=1
coin_edge[(edge_list[i][1],rep_string.format(j))]=1
#------Assigning Transition circuit to position states-----#
coin_states={}
rep_string="{0:0"+str(c_cq)+"b}"
for i in range(0,int(math.pow(2,c_cq))-1):
coin_states[rep_string.format(i)]=[]
for i in range(0,len(edge_list)):
coin_states[edge_list[i][3]].append(i)
rep_string="{0:0"+str(c_pq-1)+"b}"
qc_l2=[]
gate_apply=[]
for i in range(c_pq,c_pq+c_aq):
gate_apply.append(i)
for i in range(0,c_pq):
gate_apply.append(i)

for coin_value,circs in coin_states.items():
if len(circs)==0:
continue
for i in circs:
qc_temp=QuantumCircuit(c_pq+c_aq,name='Transition circuit: Coin state '+str(int(coin_value)))
for j in range(0,c_pq-1):
qc_temp.cx(j,c_pq+j)
v1=rep_string.format(edge_list[i][0])
v1=v1[::-1] #string reverse
for i in range(0,len(v1)):
if(v1[i]=='0'):
qc_temp.x(c_pq+i)
gate_tmp=qc_l3[i].to_gate().control(c_aq)
qc_temp.append(gate_tmp,gate_apply)
for i in range(0,len(v1)):
if(v1[i]=='0'):
qc_temp.x(c_pq+i)
v2=rep_string.format(edge_list[i][1])
v2=v2[::-1]
for i in range(0,len(v2)):
if(v2[i]=='0'):
qc_temp.x(c_pq+i)
qc_temp.cx(c_pq-1,c_pq)
gate_tmp=qc_l3[i].to_gate().control(c_aq)
qc_temp.append(gate_tmp,gate_apply)
qc_l2.append([coin_value,qc_temp])
#-----------------Making Final Circuit with Coin assigned to each transition---------------->
qc_l3=QuantumCircuit(c_pq+c_aq+c_cq,c_pq-1)
gate_apply=[]
for i in range(c_pq+c_aq,c_pq+c_aq+c_cq):
qc_l3.h(i)
gate_apply.append(i)
for i in range(0,c_pq+c_aq):
gate_apply.append(i)
for cc in qc_l2:      #cc=coin_circuit
coin_value=cc[0]
print('ok')
coin_value=coin_value[::-1]
circ=cc[1]
gate_tmp=circ.to_gate().control(c_cq)
for j in range(c_pq,c_pq+c_aq):
qc_temp.initialize(i_zero,j)
for i in range(0,len(coin_value)):
if(coin_value[i]=='0'):
qc_l3.x(c_pq+c_aq+i)
qc_l3.append(gate_tmp,gate_apply)
for i in range(0,len(coin_value)):
if(coin_value[i]=='0'):
qc_l3.x(c_pq+c_aq+i)
qc_l3.draw('mpl')

$$$$
`
• Can you share your code? I tested to_gate() with a 28 qubits circuit that contains more than 50 2-qubit gates and 50 1-qubit gate. I took only 2 milliseconds on my laptop! Mar 27, 2021 at 12:50
• Does your circuit had any controled operations? Mar 27, 2021 at 13:22
• Internally, control() method unrolls your "composite gate" into basic gates (cx, x, z, rx, ...etc.) and converts them into controlled gates one by one. Now, your composite gate is huge. It is unrolled into about 2300 basic gates. Mar 27, 2021 at 18:42
• Another factor that leads to the bad performance of control() method is the number of control qubits. Try to reduce this number by using ancilla. Mar 28, 2021 at 18:04
• Ok. Thanks for help. Mar 28, 2021 at 19:36