Note

This is the documentation for the current state of the development branch of Qiskit Experiments. The documentation or APIs here can change prior to being released.

Quantum State Tomography

Quantum tomography is an experimental procedure to reconstruct a description of part of a quantum system from the measurement outcomes of a specific set of experiments. In particular, quantum state tomography reconstructs the density matrix of a quantum state by preparing the state many times and measuring them in a tomographically complete basis of measurement operators.

Note

This tutorial requires the qiskit-aer and qiskit-ibm-runtime packages to run simulations. You can install them with python -m pip install qiskit-aer qiskit-ibm-runtime.

We first initialize a simulator to run the experiments on.

from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakePerth

backend = AerSimulator.from_backend(FakePerth())

To run a state tomography experiment, we initialize the experiment with a circuit to prepare the state to be measured. We can also pass in an Operator or a Statevector to describe the preparation circuit.

import qiskit
from qiskit_experiments.framework import ParallelExperiment
from qiskit_experiments.library import StateTomography

# GHZ State preparation circuit
nq = 2
qc_ghz = qiskit.QuantumCircuit(nq)
qc_ghz.h(0)
qc_ghz.s(0)
for i in range(1, nq):
    qc_ghz.cx(0, i)

# QST Experiment
qstexp1 = StateTomography(qc_ghz)
qstdata1 = qstexp1.run(backend, seed_simulation=100).block_for_results()

# Print results
display(qstdata1.analysis_results(dataframe=True))
name experiment components value quality backend run_time trace eigvals raw_eigvals rescaled_psd fitter_metadata conditional_probability positive
adec4dea state StateTomography [Q0, Q1] DensityMatrix([[ 0.47542318+0.j        , -0.01... None aer_simulator_from(fake_perth) None 1.0 [0.9084938888651567, 0.050487060056535776, 0.0... [0.9084938888651567, 0.050487060056535776, 0.0... False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
bcd509f7 state_fidelity StateTomography [Q0, Q1] 0.908203 None aer_simulator_from(fake_perth) None None None None None None None None
e3b90380 positive StateTomography [Q0, Q1] True None aer_simulator_from(fake_perth) None None None None None None None None

Tomography Results

The main result for tomography is the fitted state, which is stored as a DensityMatrix object:

state_result = qstdata1.analysis_results("state", dataframe=True).iloc[0]
print(state_result.value)
DensityMatrix([[ 0.47542318+0.j        , -0.01855469-0.00764974j,
                -0.00146484+0.00048828j,  0.00732422-0.43798828j],
               [-0.01855469+0.00764974j,  0.02783203+0.j        ,
                -0.01123047+0.00244141j,  0.00244141-0.00537109j],
               [-0.00146484-0.00048828j, -0.01123047-0.00244141j,
                 0.03173828+0.j        ,  0.00878906-0.00374349j],
               [ 0.00732422+0.43798828j,  0.00244141+0.00537109j,
                 0.00878906+0.00374349j,  0.46500651+0.j        ]],
              dims=(2, 2))

We can also visualize the density matrix:

from qiskit.visualization import plot_state_city
state = qstdata1.analysis_results("state", dataframe=True).iloc[0].value
plot_state_city(state, title='Density Matrix')
../../_images/state_tomography_3_0.png

The state fidelity of the fitted state with the ideal state prepared by the input circuit is stored in the "state_fidelity" result field. Note that if the input circuit contained any measurements the ideal state cannot be automatically generated and this field will be set to None.

fid_result = qstdata1.analysis_results("state_fidelity", dataframe=True).iloc[0]
print("State Fidelity = {:.5f}".format(fid_result.value))
State Fidelity = 0.90820

Additional state metadata

Additional data is stored in the tomography under additional fields. This includes

  • eigvals: the eigenvalues of the fitted state

  • trace: the trace of the fitted state

  • positive: Whether the eigenvalues are all non-negative

If trace rescaling was performed this dictionary will also contain a raw_trace field containing the trace before rescaling. Futhermore, if the state was rescaled to be positive or trace 1 an additional field raw_eigvals will contain the state eigenvalues before rescaling was performed.

for col in ["eigvals", "trace", "positive"]:
    print(f"{col}: {state_result[col]}")
eigvals: [0.90849389 0.05048706 0.03447817 0.00654088]
trace: 1.0000000000000013
positive: True

To see the effect of rescaling, we can perform a “bad” fit with very low counts:

# QST Experiment
bad_data = qstexp1.run(backend, shots=10, seed_simulation=100).block_for_results()
bad_state_result = bad_data.analysis_results("state", dataframe=True).iloc[0]

# Print result
for key, val in bad_state_result.items():
    print(f"{key}: {val}")
name: state
experiment: StateTomography
components: [<Qubit(Q0)>, <Qubit(Q1)>]
value: DensityMatrix([[0.49887846+0.00000000e+00j, 0.01599749-7.16227381e-02j,
                0.01254335-3.70842058e-02j, 0.01052513-4.13870552e-01j],
               [0.01599749+7.16227381e-02j, 0.06050889+0.00000000e+00j,
                0.02262362+1.73112398e-03j, 0.02091374+4.18688252e-02j],
               [0.01254335+3.70842058e-02j, 0.02262362-1.73112398e-03j,
                0.00884057-4.33680869e-19j, 0.01903516+9.47944987e-03j],
               [0.01052513+4.13870552e-01j, 0.02091374-4.18688252e-02j,
                0.01903516-9.47944987e-03j, 0.43177208+0.00000000e+00j]],
              dims=(2, 2))
quality: None
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 1.0
eigvals: [0.88858524 0.11141476 0.         0.        ]
raw_eigvals: [ 1.01008887  0.23291839 -0.0262003  -0.21680696]
rescaled_psd: True
fitter_metadata: {'fitter': 'linear_inversion', 'fitter_time': 0.0032052993774414062}
conditional_probability: 1.0
positive: True

Tomography Fitters

The default fitters is linear_inversion, which reconstructs the state using dual basis of the tomography basis. This will typically result in a non-positive reconstructed state. This state is rescaled to be positive-semidefinite (PSD) by computing its eigen-decomposition and rescaling its eigenvalues using the approach from Ref. [1].

There are several other fitters are included (See API documentation for details). For example, if cvxpy is installed we can use the cvxpy_gaussian_lstsq() fitter, which allows constraining the fit to be PSD without requiring rescaling.

try:
    import cvxpy

    # Set analysis option for cvxpy fitter
    qstexp1.analysis.set_options(fitter='cvxpy_gaussian_lstsq')

    # Re-run experiment
    qstdata2 = qstexp1.run(backend, seed_simulation=100).block_for_results()

    state_result2 = qstdata2.analysis_results("state", dataframe=True).iloc[0]
    for key, val in state_result2.items():
        print(f"{key}: {val}")

except ModuleNotFoundError:
    print("CVXPY is not installed")
name: state
experiment: StateTomography
components: [<Qubit(Q0)>, <Qubit(Q1)>]
value: DensityMatrix([[ 0.48314624+0.00000000e+00j,  0.00812233-6.38337813e-03j,
                 0.01014687-8.58614886e-04j, -0.01308046-4.36623533e-01j],
               [ 0.00812233+6.38337813e-03j,  0.01937327+0.00000000e+00j,
                 0.0022028 -1.06422701e-02j, -0.01244875+2.28804898e-04j],
               [ 0.01014687+8.58614886e-04j,  0.0022028 +1.06422701e-02j,
                 0.02956154+0.00000000e+00j, -0.007024  -1.50610452e-03j],
               [-0.01308046+4.36623533e-01j, -0.01244875-2.28804898e-04j,
                -0.007024  +1.50610452e-03j,  0.46791895+0.00000000e+00j]],
              dims=(2, 2))
quality: None
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 0.999999999868219
eigvals: [0.91257886 0.04993698 0.03146919 0.00601496]
raw_eigvals: [0.91257886 0.04993698 0.03146919 0.00601496]
rescaled_psd: False
fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.03708600997924805}
conditional_probability: 1.0
positive: True

Parallel Tomography Experiment

We can also use the ParallelExperiment class to run subsystem tomography on multiple qubits in parallel.

For example if we want to perform 1-qubit QST on several qubits at once:

from math import pi
num_qubits = 5
gates = [qiskit.circuit.library.RXGate(i * pi / (num_qubits - 1))
         for i in range(num_qubits)]

subexps = [
    StateTomography(gate, physical_qubits=(i,))
    for i, gate in enumerate(gates)
]
parexp = ParallelExperiment(subexps)
pardata = parexp.run(backend, seed_simulation=100).block_for_results()

display(pardata.analysis_results(dataframe=True))
name experiment components value quality backend run_time trace eigvals raw_eigvals rescaled_psd fitter_metadata conditional_probability positive
43c524da state StateTomography [Q0] DensityMatrix([[ 0.97167969+0.j        , -0.01... None aer_simulator_from(fake_perth) None 1.0 [0.9720485342902634, 0.027951465709737618] [0.9720485342902634, 0.027951465709737618] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
98fbe378 state_fidelity StateTomography [Q0] 0.97168 None aer_simulator_from(fake_perth) None None None None None None None None
98614e66 positive StateTomography [Q0] True None aer_simulator_from(fake_perth) None None None None None None None None
3fd02ce5 state StateTomography [Q1] DensityMatrix([[ 0.84570312+0.j       , -0.021... None aer_simulator_from(fake_perth) None 1.0 [0.9717757164799353, 0.028224283520065493] [0.9717757164799353, 0.028224283520065493] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
ea3f20eb state_fidelity StateTomography [Q1] 0.970944 None aer_simulator_from(fake_perth) None None None None None None None None
0e7ba192 positive StateTomography [Q1] True None aer_simulator_from(fake_perth) None None None None None None None None
b0d5ba93 state StateTomography [Q2] DensityMatrix([[0.49707031+0.j        , 0.0224... None aer_simulator_from(fake_perth) None 1.0 [0.9595429612956416, 0.0404570387043591] [0.9595429612956416, 0.0404570387043591] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
1be05524 state_fidelity StateTomography [Q2] 0.958984 None aer_simulator_from(fake_perth) None None None None None None None None
2707e0ea positive StateTomography [Q2] True None aer_simulator_from(fake_perth) None None None None None None None None
407472f4 state StateTomography [Q3] DensityMatrix([[ 0.16015625+0.j        , -0.00... None aer_simulator_from(fake_perth) None 1.0 [0.9630101045696455, 0.036989895430355565] [0.9630101045696455, 0.036989895430355565] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
7536f467 state_fidelity StateTomography [Q3] 0.962658 None aer_simulator_from(fake_perth) None None None None None None None None
70e00544 positive StateTomography [Q3] True None aer_simulator_from(fake_perth) None None None None None None None None
8bf566fb state StateTomography [Q4] DensityMatrix([[0.04003906+0.j        , 0.    ... None aer_simulator_from(fake_perth) None 1.0 [0.9607729860679133, 0.039227013932087634] [0.9607729860679133, 0.039227013932087634] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
64ff34cf state_fidelity StateTomography [Q4] 0.959961 None aer_simulator_from(fake_perth) None None None None None None None None
d603529f positive StateTomography [Q4] True None aer_simulator_from(fake_perth) None None None None None None None None

View experiment analysis results for one component:

results = pardata.analysis_results(dataframe=True)
display(results[results.components.apply(lambda x: x == ["Q0"])])
name experiment components value quality backend run_time trace eigvals raw_eigvals rescaled_psd fitter_metadata conditional_probability positive
43c524da state StateTomography [Q0] DensityMatrix([[ 0.97167969+0.j        , -0.01... None aer_simulator_from(fake_perth) None 1.0 [0.9720485342902634, 0.027951465709737618] [0.9720485342902634, 0.027951465709737618] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
98fbe378 state_fidelity StateTomography [Q0] 0.97168 None aer_simulator_from(fake_perth) None None None None None None None None
98614e66 positive StateTomography [Q0] True None aer_simulator_from(fake_perth) None None None None None None None None

References

See also