# How to obtain the density matrix using tomography in the real device?

I am trying to run the QFT algorithm for n=5 (n number of qubits). The number of experiments that it generates is bigger than the one allowed by the IBM devices (i.e. 75). Therefore, I have tried to used the IBMQJobManager but this is not working as my result is not the counts but the density matrix obtain from the tomography process. Is it possible to make it work? I have made the following attempt (qft is a function that I have created that implements the QFT algorithm):

n=5
beta=0.5*np.pi
state=initial_state_QFT(n,beta)
qr=QuantumRegister(n)
cr=ClassicalRegister(n)
qc=QuantumCircuit(qr)

qc.initialize(state,qr)
qft(qc,n)
qst = tomo.state_tomography_circuits(qc, qr)
job = job_manager.run(qst, backend=device, name='foo',shots=8192)
statefit = tomo.StateTomographyFitter(job.results(), qst)
p, M, weights = statefit._fitter_data(True, 0.5)
M_dg = np.conj(M).T
linear_inversion_matrix = np.linalg.inv(M_dg @ M) @ M_dg
rho = linear_inversion_matrix @ p
rho = np.reshape(rho, (2**n, 2**n))


IBMQJobManager.run() returns a ManagedResult, but StateTomographyFitter expects a Result object. Unfortunately there is no easy way to convert the former into the latter, so you'll have to combine the results yourself:

job_set = job_manager.run(qst, backend=device, name='foo',shots=8192)
jobs = job_set.jobs()
new_result = copy.deepcopy(jobs[0].result())
for idx in range(1, len(jobs)):
new_result.results.extend(jobs[idx].result().results)
statefit = tomo.StateTomographyFitter(new_result, qst)


This is an interesting use case. Looks like Job Manager should really support returning a Result object.

• Thank you for your help, my only question is in the last line, where you have written job.results() it should be something else as no variable called job is defined. Jun 2, 2020 at 8:45
• Oops copy & paste error. Updated to use the new_result that has combined results from all jobs. Jun 3, 2020 at 12:47
• Thank you again. I still have a problem because when I run the code I get the following error: The Qobj uses gates (['h', 'sdg']) that are not among the basis gates (['u1', 'u2', 'u3', 'cx', 'id']) [1106]. I have unrolled the quantum circuit object in these basis gates and I have checked that it has been done propertly. Anyway it does not work. Jun 4, 2020 at 13:39
• Unlike execute, IBMQJobManager.run() doesn't transpile the circuits for you (e.g. no gate unrolling), so you'll have to do that yourself. Can you check that all circuits in qst have been unrolled to the basis gates? Jun 5, 2020 at 14:31
• I changed this: pass_ = Unroller(['u1', 'u2', 'u3', 'cx', 'id']) pm = PassManager([pass_]) circ = pm.run(qc) qst = tomo.state_tomography_circuits(circ, qr) Jun 5, 2020 at 19:21