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
77dd3bfb state StateTomography [Q0, Q1] DensityMatrix([[ 0.48209635+0.j        ,  0.00... None aer_simulator_from(fake_perth) None 1.0 [0.9158026313997578, 0.037075201249992, 0.0286... [0.9158026313997578, 0.037075201249992, 0.0286... False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
899b97a0 state_fidelity StateTomography [Q0, Q1] 0.915527 None aer_simulator_from(fake_perth) None None None None None None None None
3925dec6 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.48209635+0.j        ,  0.00537109-0.00585938j,
                 0.00423177+0.00406901j, -0.00830078-0.44287109j],
               [ 0.00537109+0.00585938j,  0.0250651 +0.j        ,
                 0.00146484+0.00048828j, -0.00553385-0.00472005j],
               [ 0.00423177-0.00406901j,  0.00146484-0.00048828j,
                 0.0296224 +0.j        , -0.00341797+0.00097656j],
               [-0.00830078+0.44287109j, -0.00553385+0.00472005j,
                -0.00341797-0.00097656j,  0.46321615+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.91553

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.91580263 0.0370752  0.02861642 0.01850574]
trace: 1.000000000000002
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.42956174+0.00000000e+00j,  0.04638249-1.06214618e-01j,
                 0.03322422+3.89358891e-03j,  0.10239843-4.13528550e-01j],
               [ 0.04638249+1.06214618e-01j,  0.06144443+0.00000000e+00j,
                -0.04280106+1.73791166e-02j,  0.12821419-3.23289133e-02j],
               [ 0.03322422-3.89358891e-03j, -0.04280106-1.73791166e-02j,
                 0.07352695+3.46944695e-18j, -0.02203754-1.76653020e-02j],
               [ 0.10239843+4.13528550e-01j,  0.12821419+3.23289133e-02j,
                -0.02203754+1.76653020e-02j,  0.43546688+1.38777878e-17j]],
              dims=(2, 2))
quality: None
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 1.0000000000000016
eigvals: [0.89765194 0.10234806 0.         0.        ]
raw_eigvals: [ 0.95351206  0.15820817  0.03102995 -0.14275019]
rescaled_psd: True
fitter_metadata: {'fitter': 'linear_inversion', 'fitter_time': 0.003979682922363281}
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([[ 4.66028037e-01+0.j        ,  5.91425123e-03+0.00439595j,
                -2.64493884e-03+0.00762362j, -6.35438076e-03-0.44530185j],
               [ 5.91425123e-03-0.00439595j,  3.00105630e-02+0.j        ,
                -8.22753605e-03-0.00476203j,  3.40392861e-03-0.00824082j],
               [-2.64493884e-03-0.00762362j, -8.22753605e-03+0.00476203j,
                 2.86423419e-02+0.j        ,  6.49087193e-05-0.00486837j],
               [-6.35438076e-03+0.44530185j,  3.40392861e-03+0.00824082j,
                 6.49087193e-05+0.00486837j,  4.75319058e-01+0.j        ]],
              dims=(2, 2))
quality: None
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 0.9999999997995237
eigvals: [0.91619095 0.03893877 0.03235541 0.01251487]
raw_eigvals: [0.91619095 0.03893877 0.03235541 0.01251487]
rescaled_psd: False
fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.03820323944091797}
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
0a39d8b4 state StateTomography [Q0] DensityMatrix([[0.96875   +0.j        , 0.0458... None aer_simulator_from(fake_perth) None 1.0 [0.9709927629372428, 0.029007237062758148] [0.9709927629372428, 0.029007237062758148] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
b6d05535 state_fidelity StateTomography [Q0] 0.96875 None aer_simulator_from(fake_perth) None None None None None None None None
550ac865 positive StateTomography [Q0] True None aer_simulator_from(fake_perth) None None None None None None None None
148c08ac state StateTomography [Q1] DensityMatrix([[ 0.84570312+0.j        , -0.01... None aer_simulator_from(fake_perth) None 1.0 [0.989016048675109, 0.010983951324891938] [0.989016048675109, 0.010983951324891938] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
df67bc0c state_fidelity StateTomography [Q1] 0.988898 None aer_simulator_from(fake_perth) None None None None None None None None
24e103cc positive StateTomography [Q1] True None aer_simulator_from(fake_perth) None None None None None None None None
0a85fdd4 state StateTomography [Q2] DensityMatrix([[0.52832031+0.j        , 0.0166... None aer_simulator_from(fake_perth) None 1.0 [0.972820663230402, 0.02717933676959905] [0.972820663230402, 0.02717933676959905] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
4388cd84 state_fidelity StateTomography [Q2] 0.97168 None aer_simulator_from(fake_perth) None None None None None None None None
7b40324e positive StateTomography [Q2] True None aer_simulator_from(fake_perth) None None None None None None None None
444e44ae state StateTomography [Q3] DensityMatrix([[0.16503906+0.j        , 0.0039... None aer_simulator_from(fake_perth) None 1.0 [0.9695976223107218, 0.030402377689279517] [0.9695976223107218, 0.030402377689279517] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
52012119 state_fidelity StateTomography [Q3] 0.969563 None aer_simulator_from(fake_perth) None None None None None None None None
3ff9c8de positive StateTomography [Q3] True None aer_simulator_from(fake_perth) None None None None None None None None
915b6993 state StateTomography [Q4] DensityMatrix([[0.03710938+0.j        , 0.0292... None aer_simulator_from(fake_perth) None 1.0 [0.9638178427701689, 0.03618215722983254] [0.9638178427701689, 0.03618215722983254] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
5f3a124f state_fidelity StateTomography [Q4] 0.962891 None aer_simulator_from(fake_perth) None None None None None None None None
e310b48a 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
0a39d8b4 state StateTomography [Q0] DensityMatrix([[0.96875   +0.j        , 0.0458... None aer_simulator_from(fake_perth) None 1.0 [0.9709927629372428, 0.029007237062758148] [0.9709927629372428, 0.029007237062758148] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
b6d05535 state_fidelity StateTomography [Q0] 0.96875 None aer_simulator_from(fake_perth) None None None None None None None None
550ac865 positive StateTomography [Q0] True None aer_simulator_from(fake_perth) None None None None None None None None

References

See also