11

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.

blunova
  • 201
  • 1
  • 3
  • 11
asdf
  • 503
  • 3
  • 15

2 Answers2

3

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.

Ali Javadi
  • 1,652
  • 1
  • 10
  • 11
0

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.