• Randomizerpanorama_fish_eye
    9 posts
    4 years

    I have a quantum circuit with a series of quantum gates. Is there an easy way to implement the same series of quantum gates to another quantum circuit?

    For example

    qc1 = QuantumCircuit(1)
    qc1.h(0)
    qc1.x(0)
    qc1.y(0)
    
    qc2 = QuantumCircuit(2)
    

    How to easily copy the gate operations in qc1 to the 1st qubit of qc2?

  • CattyMpanorama_fish_eye
    17 posts
    4 years

    You can use the to_instruction() method to assemble the series of quantum gates into one gate, then append it to other quantum circuits.

    from qiskit import QuantumCircuit
    
    qc1 = QuantumCircuit(1)
    qc1.h(0)
    qc1.x(0)
    qc1.y(0)
    custom_gate = qc1.to_instruction()
    
    qc2 = QuantumCircuit(2)
    qc2.append(custom_gate, [0])
    
  • JXWpanorama_fish_eye
    19 posts
    4 years
    • If 2 quantum circuits have the same number of qubits, there is an easy way to do it, simply by adding these two quantum circuits. For example:
    from qiskit import QuantumCircuit
    
    qc1 = QuantumCircuit(2)
    qc2 = QuantumCircuit(2)
    
    qc1.h(0)
    qc1.cx(1,0)
    
    qc2.x(0)
    
    qc3 = qc1 + qc2
    

    qc3 is a circuit which applies the gates in qc1 then the gates in qc2.

    • If the two circuits have different number of qubits, you can use QuantumRegister to name your qubits, then qiskit will add the circuits by matching the same qubit names. For example:
    from qiskit import QuantumCircuit
    from qiskit import QuantumRegister
    qr1_a = QuantumRegister(1, 'a')
    qr1_b = QuantumRegister(2, 'b')
    qc1 = QuantumCircuit(qr1_a, qr1_b)
    
    qc2_b = QuantumRegister(2,'b')
    qc2 = QuantumCircuit(qc2_b)
    
    qc1.cx(1,0)
    
    qc2.x(1)
    qc2.z(0)
    
    qc3 = qc1 + qc2
    

    where qc3 is the circuit with qc2being added behind qc1's gates on the 'b' qubits.

There are no more posts in this thread.

first_page
chevron_left
chevron_right
last_page