I was wondering if there is a way to compose a program with multiple quantum circuits without having the register reinitialized at $0$ for each circuit.

Specifically, I would like run a second quantum circuit after running the first one, as in this example:

qp = QuantumProgram()
qr = qp.create_quantum_register('qr',2)
cr = qp.create_classical_register('cr',2)

qc1 = qp.create_circuit('B1',[qr],[cr])

qc1.measure(qr[0], cr[0])
qc1.measure(qr[1], cr[1])

qc2 = qp.create_circuit('B2', [qr], [cr])
qc2.measure(qr[0], cr[0])
qc2.measure(qr[1], cr[1])

#qp.add_circuit('B1', qc1)
#qp.add_circuit('B2', qc2)


result = qp.execute()


Unfortunately, what I get is the same result for the two runs (i.e. a count of 11 for the B1 and B2 instead of 11 and 00 for the second, as if B2 is run on a completely new state initialized on 00 after B1.

  • 1
    $\begingroup$ So could I understand it as you wanting one long circuit composed of multiple parts, and you want to be able to see the output after each part? $\endgroup$ – James Wootton Jun 6 '18 at 6:37
  • $\begingroup$ yes. Imagine I have a code base that gives me circuits, and I want to be able to compose them as a puzzle :) $\endgroup$ – asdf Jun 6 '18 at 11:47
  • $\begingroup$ (the measure is there just to show you that is not the expected behavior) $\endgroup$ – asdf Jun 6 '18 at 11:48

In Qiskit you can compose two circuits to make a bigger circuit. You can do this simply by using the + operator on the circuits.

Here is your program rewritten to illustrate this (note: you need the latest version of Qiskit for this, upgrade with pip install -U qiskit).

from qiskit import *
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc1 = QuantumCircuit(qr, cr)

qc2 = QuantumCircuit(qr, cr)

qc3 = qc1 + qc2

You can see that qc3 is a concatenation of q1 and q2.



include "qelib1.inc";
qreg q0[2];
creg c0[2];
x q0[0];
x q0[1];
x q0[0];
x q0[1];

Now, you seem to want to probe the state twice: once where qc1 ends, and once when qc2 ends. You can do this in a simulator by inserting snapshot commands. This will save the statevector at a given point in the circuit. It does not collapse the state.

from qiskit.extensions.simulator import *
qc1.snapshot('0')    # save the snapshot in slot "0"
qc2.snapshot('1')    # save the snapshot in slot "1"
qc2.measure(qr, cr)  # measure to get final counts

qc3 = qc1 + qc2

You can now execute qc3 on a simulator.

job = execute(qc3, 'local_qasm_simulator')
result = job.result()

Yields: [0.+0.j 0.+0.j 0.+0.j 1.+0.j] [1.+0.j 0.+0.j 0.+0.j 0.+0.j] {'00': 1024}

So the state goes back to |00> as expected.

  • $\begingroup$ snapshot only works on the simulator and not on the real thing, right? (no cloning thingy) $\endgroup$ – lalala Jan 2 at 20:51

Once you do a measurement, the wavefunction of the quantum state/register collapses and it loses its quantum nature. It doesn't make sense to apply another circuit on it.

  • $\begingroup$ Sure sure, but if I remove the measure, I would expect to get 00, while I get 1. The measure is just to show that I want to start the second circuit with the qubits initialized to 11. $\endgroup$ – asdf Jun 5 '18 at 22:36
  • $\begingroup$ Indeed I want my wavefunction to collapse in quantum state and not 00 if you want it put in that way... $\endgroup$ – asdf Jun 5 '18 at 22:37
  • 2
    $\begingroup$ @Vidya I understand that it collapses but I don't agree it would not make sense to apply another circuit on the collapsed output of a previous circuit. $\endgroup$ – JanVdA Jun 6 '18 at 6:09

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.