# Make a controlled gate from a QuantumCircuit that uses append in Qiskit

Here is a minimal example for what I want to do:

from qiskit import QuantumCircuit

qc1 = QuantumCircuit(1, name='qc1')
qc1.x(0)

qc2 = QuantumCircuit(1, name='qc2')
qc2.h(0)
qc2.append(qc1, [0])
qc2.z(0)

c_qc2 = qc2.to_gate().control(1)


This gives the following error:

qiskit.exceptions.QiskitError: 'One or more instructions cannot be converted to a gate. "qc1" is not a gate instruction'

I'm aware that in this example I can get the desired controlled gate of qc2 by manually adding a control qubit to each gate in qc2. However in practice qc2 would be complex and doing so would be tiresome.

I also tried qc2.append(qc1.to_instruction(), [0]) which didn't solve the problem. Any help is appreciated. Thanks!

• qc2.append(qc1.to_gate(), [0]) Commented Mar 24, 2021 at 20:14
• Thank you so much! Commented Mar 25, 2021 at 0:25

Edit: Miss understood your question earlier. As commented by @Egrettal you can do the following.

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

qc1 = QuantumCircuit(1, name='qc1')
qc1.x(0)
print(qc1)

qc2 = QuantumCircuit(1, name='qc2')
qc2.h(0)
qc2.append(qc1.to_gate(), [0])
qc2.z(0)
print(qc2)

xs_gate = qc2.to_gate()
cxs_gate = xs_gate.control()
circuit = QuantumCircuit(2)
circuit.append(cxs_gate, [0,1])
print('\n New circuit with controlled:\n',circuit)
print('\n Decomposed new circuit:\n', circuit.decompose())


Which will output:

     ┌───┐
q_0: ┤ X ├
└───┘

┌───┐┌─────┐┌───┐
q_0: ┤ H ├┤ qc1 ├┤ Z ├
└───┘└─────┘└───┘

New circuit with controlled:

q_0: ───■───
┌──┴──┐
q_1: ┤ qc2 ├
└─────┘

Decomposed new circuit:
┌────────┐                                                     »
q_0: ┤ P(π/2) ├──■───────────────────■───────────────────────────■──»
├────────┤┌─┴─┐┌─────────────┐┌─┴─┐┌──────────┐┌─────────┐┌─┴─┐»
q_1: ┤ P(π/2) ├┤ X ├┤ U(0,0,-π/2) ├┤ X ├┤ U(0,0,0) ├┤ RY(π/4) ├┤ X ├»
└────────┘└───┘└─────────────┘└───┘└──────────┘└─────────┘└───┘»
«                      ┌──────┐                                       »
«q_0: ──────────────■──┤ P(0) ├──■────────────────■────────────────■──»
«     ┌──────────┐┌─┴─┐├──────┤┌─┴─┐┌──────────┐┌─┴─┐┌──────────┐┌─┴─┐»
«q_1: ┤ RY(-π/4) ├┤ X ├┤ P(0) ├┤ X ├┤ U(0,0,0) ├┤ X ├┤ U(0,0,0) ├┤ X ├»
«     └──────────┘└───┘└──────┘└───┘└──────────┘└───┘└──────────┘└───┘»
«     ┌────────┐
«q_0: ┤ P(π/2) ├──■───────────────────■──────────────
«     ├────────┤┌─┴─┐┌─────────────┐┌─┴─┐┌──────────┐
«q_1: ┤ P(π/2) ├┤ X ├┤ U(0,0,-π/2) ├┤ X ├┤ U(0,0,0) ├
«     └────────┘└───┘└─────────────┘└───┘└──────────┘


• Thank you very much for your answer! However this doesn't quite solve my problem. I have edited the question to make it more clear. The reason this doesn't work is because when qc2 have more gates than just what qc1 has, in order to get a controlled qc2, I'll have to also manually control all other gates as well. Commented Mar 24, 2021 at 19:37
• Thank you so much! Can you tell me the difference between a gate an instruction in qiskit? Or at least give a link I can read into it. Commented Mar 25, 2021 at 0:27
• Here is the circuit library for qiskit: qiskit.org/documentation/apidoc/circuit_library.html All the gates are there. Maybe putting in more details and make it as a new question so someone from the qiskit dev team can help. Commented Mar 25, 2021 at 2:47