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
5115fe3a state StateTomography [Q0, Q1] DensityMatrix([[ 0.48316105+0.j        , -0.00... unknown aer_simulator_from(fake_perth) None 1.0 [0.9203778291485829, 0.05119856227860111, 0.02... [0.9216995976352799, 0.052520330765298065, 0.0... True {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
65760872 state_fidelity StateTomography [Q0, Q1] 0.91958 unknown aer_simulator_from(fake_perth) None None None None None None None None
02582865 positive StateTomography [Q0, Q1] True unknown 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.48316105+0.j        , -0.00423092-0.00993102j,
                -0.00329712+0.00149168j,  0.01801373-0.44761941j],
               [-0.00423092+0.00993102j,  0.02387842+0.j        ,
                 0.02013869+0.00327687j,  0.00854465-0.01325155j],
               [-0.00329712-0.00149168j,  0.02013869-0.00327687j,
                 0.03220121+0.j        ,  0.00868643+0.00540577j],
               [ 0.01801373+0.44761941j,  0.00854465+0.01325155j,
                 0.00868643-0.00540577j,  0.46075933+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.91958

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.92037783 0.05119856 0.02842361 0.        ]
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.35054117+0.j        ,  0.01721209-0.03473756j,
                -0.08414603-0.07482385j,  0.13530308-0.35704869j],
               [ 0.01721209+0.03473756j,  0.07351822+0.j        ,
                -0.05122913-0.00368611j,  0.07705656+0.06150771j],
               [-0.08414603+0.07482385j, -0.05122913+0.00368611j,
                 0.08009463+0.j        ,  0.02404443+0.05869772j],
               [ 0.13530308+0.35704869j,  0.07705656-0.06150771j,
                 0.02404443-0.05869772j,  0.49584598+0.j        ]],
              dims=(2, 2))
quality: unknown
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 1.0
eigvals: [0.84152086 0.15847914 0.         0.        ]
raw_eigvals: [ 0.97349898  0.29045726 -0.0325685  -0.23138774]
rescaled_psd: True
fitter_metadata: {'fitter': 'linear_inversion', 'fitter_time': 0.0037674903869628906}
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.45863494+0.00000000e+00j,  0.00046834+2.54244188e-04j,
                -0.01626546+1.70562386e-02j, -0.01478818-4.40366323e-01j],
               [ 0.00046834-2.54244188e-04j,  0.03362366+0.00000000e+00j,
                 0.00762422-8.81661955e-03j,  0.01144632-2.10458929e-02j],
               [-0.01626546-1.70562386e-02j,  0.00762422+8.81661955e-03j,
                 0.04921901+0.00000000e+00j,  0.00647467-5.90372256e-03j],
               [-0.01478818+4.40366323e-01j,  0.01144632+2.10458929e-02j,
                 0.00647467+5.90372256e-03j,  0.45852239+0.00000000e+00j]],
              dims=(2, 2))
quality: unknown
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 0.9999999986941865
eigvals: [0.899657   0.06999205 0.0287167  0.00163425]
raw_eigvals: [0.899657   0.06999205 0.0287167  0.00163425]
rescaled_psd: False
fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.03876996040344238}
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
4469a58f state StateTomography [Q0] DensityMatrix([[ 0.97949219+0.j        , -0.04... unknown aer_simulator_from(fake_perth) None 1.0 [0.9824910596898806, 0.01750894031012028] [0.9824910596898806, 0.01750894031012028] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
e6ae3fa9 state_fidelity StateTomography [Q0] 0.979492 unknown aer_simulator_from(fake_perth) None None None None None None None None
24abd4b0 positive StateTomography [Q0] True unknown aer_simulator_from(fake_perth) None None None None None None None None
c6d0a9d0 state StateTomography [Q1] DensityMatrix([[ 0.83789063+0.j        , -0.01... unknown aer_simulator_from(fake_perth) None 1.0 [0.9625381866361737, 0.03746181336382731] [0.9625381866361737, 0.03746181336382731] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
4bf6f852 state_fidelity StateTomography [Q1] 0.961967 unknown aer_simulator_from(fake_perth) None None None None None None None None
33b60fc8 positive StateTomography [Q1] True unknown aer_simulator_from(fake_perth) None None None None None None None None
569b86a6 state StateTomography [Q2] DensityMatrix([[0.48925781+0.j        , 0.0087... unknown aer_simulator_from(fake_perth) None 1.0 [0.9699315755830638, 0.03006842441693708] [0.9699315755830638, 0.03006842441693708] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
83aa56b6 state_fidelity StateTomography [Q2] 0.969727 unknown aer_simulator_from(fake_perth) None None None None None None None None
caa6f886 positive StateTomography [Q2] True unknown aer_simulator_from(fake_perth) None None None None None None None None
7f4327a9 state StateTomography [Q3] DensityMatrix([[0.19824219+0.j        , 0.0078... unknown aer_simulator_from(fake_perth) None 1.0 [0.9523603630638846, 0.04763963693611657] [0.9523603630638846, 0.04763963693611657] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
15f06ec7 state_fidelity StateTomography [Q3] 0.951609 unknown aer_simulator_from(fake_perth) None None None None None None None None
11bf0c00 positive StateTomography [Q3] True unknown aer_simulator_from(fake_perth) None None None None None None None None
9da54a74 state StateTomography [Q4] DensityMatrix([[ 0.02734375+0.j        , -0.01... unknown aer_simulator_from(fake_perth) None 1.0 [0.973988067476758, 0.02601193252324308] [0.973988067476758, 0.02601193252324308] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
768b0a1a state_fidelity StateTomography [Q4] 0.972656 unknown aer_simulator_from(fake_perth) None None None None None None None None
708341f5 positive StateTomography [Q4] True unknown 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
4469a58f state StateTomography [Q0] DensityMatrix([[ 0.97949219+0.j        , -0.04... unknown aer_simulator_from(fake_perth) None 1.0 [0.9824910596898806, 0.01750894031012028] [0.9824910596898806, 0.01750894031012028] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
e6ae3fa9 state_fidelity StateTomography [Q0] 0.979492 unknown aer_simulator_from(fake_perth) None None None None None None None None
24abd4b0 positive StateTomography [Q0] True unknown aer_simulator_from(fake_perth) None None None None None None None None

References

See also