# How do I export the rightmost state of circuit wires (qbits and cbits) in a qiskit simulation

qiskit has a quantum teleportation tutorial which winds up with this simulation code:

qasm_sim = Aer.get_backend('qasm_simulator')
t_qc = transpile(qc, qasm_sim)
qobj = assemble(t_qc)
counts = qasm_sim.run(qobj).result().get_counts()
plot_histogram(counts)


The counts are for the 3 qbits in the circuit, which also has 2 cbits. It would be most helpful to get the rightmost state of the wires in this picture for each shot:

Is there a function in qiskit which will give me these wire (cbit and qbit) output states? I looked everywhere on qiskit and with Google search and I didn't find anything.

To extract the final state, $$|\psi \rangle_{final}$$, before the measurement step, which can be represented by the circuit:

You can do:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import IBMQ, Aer, execute
import numpy as np

qreg_q = QuantumRegister(3, 'q')
circuit = QuantumCircuit(qreg_q)
vector = [0.603 + 0.625j,0.395 + 0.302j]
initial_state = vector/np.linalg.norm(vector)
circuit.initialize(initial_state, 0)
circuit.barrier(range(3))
circuit.h(qreg_q[1])
circuit.cx(qreg_q[1], qreg_q[2])
circuit.barrier(range(3))
circuit.cx(qreg_q[0], qreg_q[1])
circuit.h(qreg_q[0])
circuit.barrier(qreg_q[0], qreg_q[1], qreg_q[2])
backend = Aer.get_backend("statevector_simulator")
result = execute(circuit, backend=backend, shots=1).result()
print('Final State Vector:\n', result.get_statevector() )


Which will output the final state $$|\psi\rangle_{final}$$ as:

Final State Vector:
[ 0.30127969+0.31227166j  0.30127969+0.31227166j  0.19735569+0.15088966j
-0.19735569-0.15088966j  0.19735569+0.15088966j -0.19735569-0.15088966j
0.30127969+0.31227166j  0.30127969+0.31227166j]


Now, if you want to extract each individual counts result from an experiment, then you first need to add in the measure like you have on the circuit in your question, then change the backend option to qasm_simulator (shot based simulator instead of exact matrix manipulation). Then execute your circuit and extract the count at each shot through specifying the memory to be True.

qreg_q = QuantumRegister(3, 'q')
creg_result1 = ClassicalRegister(1, 'crz')
creg_result2 = ClassicalRegister(1, 'crx')
circuit = QuantumCircuit(qreg_q, creg_result1, creg_result2)
vector = [0.603 + 0.625j,0.395 + 0.302j]
initial_state = vector/np.linalg.norm(vector)
circuit.initialize(initial_state, 0)
circuit.barrier(range(3))

circuit.h(qreg_q[1])
circuit.cx(qreg_q[1], qreg_q[2])
circuit.barrier(range(3))
circuit.cx(qreg_q[0], qreg_q[1])
circuit.h(qreg_q[0])
circuit.barrier(qreg_q[0], qreg_q[1], qreg_q[2])
circuit.measure(qreg_q[0], creg_result1[0])
circuit.measure(qreg_q[1], creg_result2[0])
print(circuit)


backend = Aer.get_backend("qasm_simulator")
job = execute(circuit, backend=backend, shots=10, memory=True)
print('result for each shot:', job.result().get_memory() )


output is:

result for each shot: ['1 0', '0 0', '1 0', '1 0', '0 1', '1 1', '1 1', '0 0', '0 1', '1 0']

• To get q2, crz and crx in the vector should I put a measurement on the right end of the remaining 3 wires? What is the order of measurements in the vector, top to bottom? – Lars Ericson Mar 24 at 13:38