11
$\begingroup$

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.x(qr)

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

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

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

pprint(qp.get_qasms())

result = qp.execute()

print(result.get_counts('B1'))
print(result.get_counts('B2'))

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.

$\endgroup$
3
  • 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$ Jun 6, 2018 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, 2018 at 11:47
  • $\begingroup$ (the measure is there just to show you that is not the expected behavior) $\endgroup$
    – asdf
    Jun 6, 2018 at 11:48

2 Answers 2

3
$\begingroup$

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)
qc1.x(qr)

qc2 = QuantumCircuit(qr, cr)
qc2.x(qr)

qc3 = qc1 + qc2

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

print(qc3.qasm())

Yields:

OPENQASM 2.0;
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()
print(result.get_snapshot('0'))
print(result.get_snapshot('1'))
print(result.get_counts())

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.

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

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.

$\endgroup$
3
  • $\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, 2018 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, 2018 at 22:37
  • 3
    $\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, 2018 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.