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
6447fcb4 state StateTomography [Q0, Q1] DensityMatrix([[ 0.48079427+0.j        , -0.00... unknown aer_simulator_from(fake_perth) None 1.0 [0.9114715833865542, 0.05238025697051098, 0.02... [0.9114715833865542, 0.05238025697051098, 0.02... False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
03f583d1 state_fidelity StateTomography [Q0, Q1] 0.910645 unknown aer_simulator_from(fake_perth) None None None None None None None None
9feffea7 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.48079427+0.j        , -0.00341797-0.01057943j,
                 0.00976562+0.00553385j,  0.01269531-0.43554688j],
               [-0.00341797+0.01057943j,  0.0328776 +0.j        ,
                -0.00585938-0.00488281j,  0.01074219-0.00911458j],
               [ 0.00976562-0.00553385j, -0.00585938+0.00488281j,
                 0.01692708+0.j        , -0.01904297+0.00797526j],
               [ 0.01269531+0.43554688j,  0.01074219+0.00911458j,
                -0.01904297-0.00797526j,  0.46940104+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.91064

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.91147158 0.05238026 0.02990442 0.00624374]
trace: 1.0000000000000018
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([[ 4.04915760e-01+0.j        , -6.17007301e-02+0.04339756j,
                 7.47674097e-02-0.07217804j,  4.11588286e-04-0.41160974j],
               [-6.17007301e-02-0.04339756j,  6.93821847e-02+0.j        ,
                 2.87467284e-02+0.02035003j, -1.22582711e-02+0.03355759j],
               [ 7.47674097e-02+0.07217804j,  2.87467284e-02-0.02035003j,
                 7.35478441e-02+0.j        ,  9.19277923e-02-0.11114428j],
               [ 4.11588286e-04+0.41160974j, -1.22582711e-02-0.03355759j,
                 9.19277923e-02+0.11114428j,  4.52154211e-01+0.j        ]],
              dims=(2, 2))
quality: unknown
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 1.0000000000000013
eigvals: [0.88541568 0.11458432 0.         0.        ]
raw_eigvals: [ 0.95295495  0.18212359 -0.00686579 -0.12821275]
rescaled_psd: True
fitter_metadata: {'fitter': 'linear_inversion', 'fitter_time': 0.0029778480529785156}
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.48557901+0.j        , -0.00479219-0.01965894j,
                -0.00734202-0.00444223j, -0.00828272-0.43800259j],
               [-0.00479219+0.01965894j,  0.02464545+0.j        ,
                 0.01032041-0.00753095j,  0.003875  +0.01430336j],
               [-0.00734202+0.00444223j,  0.01032041+0.00753095j,
                 0.02784048+0.j        ,  0.00325173+0.01565944j],
               [-0.00828272+0.43800259j,  0.003875  -0.01430336j,
                 0.00325173-0.01565944j,  0.46193506+0.j        ]],
              dims=(2, 2))
quality: unknown
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 0.9999999997851621
eigvals: [0.91285233 0.0485892  0.02983692 0.00872156]
raw_eigvals: [0.91285233 0.0485892  0.02983692 0.00872156]
rescaled_psd: False
fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.03854703903198242}
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
a98f238f state StateTomography [Q0] DensityMatrix([[ 0.96972656+0.j       , -0.019... unknown aer_simulator_from(fake_perth) None 1.0 [0.9701973504826232, 0.02980264951737766] [0.9701973504826232, 0.02980264951737766] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
a804fb99 state_fidelity StateTomography [Q0] 0.969727 unknown aer_simulator_from(fake_perth) None None None None None None None None
488a83ae positive StateTomography [Q0] True unknown aer_simulator_from(fake_perth) None None None None None None None None
1f2a2726 state StateTomography [Q1] DensityMatrix([[ 0.84277344+0.j        , -0.00... unknown aer_simulator_from(fake_perth) None 1.0 [0.9875336073026267, 0.012466392697374137] [0.9875336073026267, 0.012466392697374137] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
d60c1e10 state_fidelity StateTomography [Q1] 0.987517 unknown aer_simulator_from(fake_perth) None None None None None None None None
60679880 positive StateTomography [Q1] True unknown aer_simulator_from(fake_perth) None None None None None None None None
d7b842c0 state StateTomography [Q2] DensityMatrix([[0.52148438+0.j        , 0.0068... unknown aer_simulator_from(fake_perth) None 1.0 [0.9634393586258461, 0.03656064137415488] [0.9634393586258461, 0.03656064137415488] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
ad8db1d5 state_fidelity StateTomography [Q2] 0.962891 unknown aer_simulator_from(fake_perth) None None None None None None None None
fa939242 positive StateTomography [Q2] True unknown aer_simulator_from(fake_perth) None None None None None None None None
fdc6fefc state StateTomography [Q3] DensityMatrix([[ 0.14941406+0.j        , -0.00... unknown aer_simulator_from(fake_perth) None 1.0 [0.9782863768458885, 0.02171362315411232] [0.9782863768458885, 0.02171362315411232] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
6414d783 state_fidelity StateTomography [Q3] 0.97785 unknown aer_simulator_from(fake_perth) None None None None None None None None
83e92511 positive StateTomography [Q3] True unknown aer_simulator_from(fake_perth) None None None None None None None None
f57af72e state StateTomography [Q4] DensityMatrix([[ 0.02148438+0.j        , -0.00... unknown aer_simulator_from(fake_perth) None 1.0 [0.9786312041539329, 0.02136879584606809] [0.9786312041539329, 0.02136879584606809] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
e04fed4c state_fidelity StateTomography [Q4] 0.978516 unknown aer_simulator_from(fake_perth) None None None None None None None None
120233c1 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
a98f238f state StateTomography [Q0] DensityMatrix([[ 0.96972656+0.j       , -0.019... unknown aer_simulator_from(fake_perth) None 1.0 [0.9701973504826232, 0.02980264951737766] [0.9701973504826232, 0.02980264951737766] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
a804fb99 state_fidelity StateTomography [Q0] 0.969727 unknown aer_simulator_from(fake_perth) None None None None None None None None
488a83ae positive StateTomography [Q0] True unknown aer_simulator_from(fake_perth) None None None None None None None None

References

See also