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
5b993c0c state StateTomography [Q0, Q1] DensityMatrix([[ 4.73144531e-01+0.j        , -... unknown aer_simulator_from(fake_perth) None 1.0 [0.9031574850901255, 0.044064677732120125, 0.0... [0.9031574850901255, 0.044064677732120125, 0.0... False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
73457769 state_fidelity StateTomography [Q0, Q1] 0.902832 unknown aer_simulator_from(fake_perth) None None None None None None None None
d7a0afc5 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([[ 4.73144531e-01+0.j        , -3.25520833e-04-0.00976563j,
                 1.30208333e-03-0.00227865j, -4.88281250e-03-0.43310547j],
               [-3.25520833e-04+0.00976563j,  2.26236979e-02+0.j        ,
                 7.81250000e-03+0.00634766j,  1.20442708e-02-0.00227865j],
               [ 1.30208333e-03+0.00227865j,  7.81250000e-03-0.00634766j,
                 3.79231771e-02+0.j        , -1.30208333e-03-0.00292969j],
               [-4.88281250e-03+0.43310547j,  1.20442708e-02+0.00227865j,
                -1.30208333e-03+0.00292969j,  4.66308594e-01+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.90283

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.90315749 0.04406468 0.03556466 0.01721318]
trace: 1.0000000000000022
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.34991546+0.00000000e+00j, -0.01732859-6.74781169e-02j,
                 0.00539769-1.63657642e-02j,  0.01046617-3.93909744e-01j],
               [-0.01732859+6.74781169e-02j,  0.07798799+0.00000000e+00j,
                 0.03042224-3.29749556e-03j,  0.00422297+6.96703239e-02j],
               [ 0.00539769+1.63657642e-02j,  0.03042224+3.29749556e-03j,
                 0.01308576+0.00000000e+00j, -0.01586531+9.36842392e-03j],
               [ 0.01046617+3.93909744e-01j,  0.00422297-6.96703239e-02j,
                -0.01586531-9.36842392e-03j,  0.55901078+3.46944695e-18j]],
              dims=(2, 2))
quality: unknown
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 0.9999999999999998
eigvals: [0.87035962 0.12964038 0.         0.        ]
raw_eigvals: [ 0.97640837  0.23568913  0.03083359 -0.24293108]
rescaled_psd: True
fitter_metadata: {'fitter': 'linear_inversion', 'fitter_time': 0.002688884735107422}
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.46986307+0.00000000e+00j, -0.00436085+2.17234885e-03j,
                -0.00629262+1.12970249e-02j,  0.01393751-4.37908395e-01j],
               [-0.00436085-2.17234885e-03j,  0.02561703+0.00000000e+00j,
                -0.00515624+3.87002519e-04j,  0.0111281 -5.79359211e-03j],
               [-0.00629262-1.12970249e-02j, -0.00515624-3.87002519e-04j,
                 0.02937675+0.00000000e+00j,  0.00230446-1.68402097e-02j],
               [ 0.01393751+4.37908395e-01j,  0.0111281 +5.79359211e-03j,
                 0.00230446+1.68402097e-02j,  0.47514315+0.00000000e+00j]],
              dims=(2, 2))
quality: unknown
backend: aer_simulator_from(fake_perth)
run_time: None
trace: 1.0000000001577074
eigvals: [0.91080321 0.05236314 0.03092311 0.00591054]
raw_eigvals: [0.91080321 0.05236314 0.03092311 0.00591054]
rescaled_psd: False
fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.03606867790222168}
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
5b9eeadc state StateTomography [Q0] DensityMatrix([[0.97558594+0.j        , 0.0332... unknown aer_simulator_from(fake_perth) None 1.0 [0.976779575624743, 0.02322042437525782] [0.976779575624743, 0.02322042437525782] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
04916ee6 state_fidelity StateTomography [Q0] 0.975586 unknown aer_simulator_from(fake_perth) None None None None None None None None
0612d4a6 positive StateTomography [Q0] True unknown aer_simulator_from(fake_perth) None None None None None None None None
5a688187 state StateTomography [Q1] DensityMatrix([[0.83203125+0.j        , 0.0078... unknown aer_simulator_from(fake_perth) None 1.0 [0.9648201559794771, 0.035179844020523834] [0.9648201559794771, 0.035179844020523834] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
5095277e state_fidelity StateTomography [Q1] 0.964729 unknown aer_simulator_from(fake_perth) None None None None None None None None
26686dfe positive StateTomography [Q1] True unknown aer_simulator_from(fake_perth) None None None None None None None None
95ddbb56 state StateTomography [Q2] DensityMatrix([[0.50585938+0.j        , 0.0244... unknown aer_simulator_from(fake_perth) None 1.0 [0.9762482153991748, 0.023751784600825998] [0.9762482153991748, 0.023751784600825998] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
5a187da0 state_fidelity StateTomography [Q2] 0.975586 unknown aer_simulator_from(fake_perth) None None None None None None None None
86279571 positive StateTomography [Q2] True unknown aer_simulator_from(fake_perth) None None None None None None None None
eca543e0 state StateTomography [Q3] DensityMatrix([[0.15917969+0.j        , 0.0224... unknown aer_simulator_from(fake_perth) None 1.0 [0.9894624384962859, 0.010537561503714751] [0.9894624384962859, 0.010537561503714751] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
4cb312d8 state_fidelity StateTomography [Q3] 0.988898 unknown aer_simulator_from(fake_perth) None None None None None None None None
24f9e419 positive StateTomography [Q3] True unknown aer_simulator_from(fake_perth) None None None None None None None None
5bd69222 state StateTomography [Q4] DensityMatrix([[0.03417969+0.j        , 0.0273... unknown aer_simulator_from(fake_perth) None 1.0 [0.9669909213570245, 0.03300907864297625] [0.9669909213570245, 0.03300907864297625] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
96f5da2b state_fidelity StateTomography [Q4] 0.96582 unknown aer_simulator_from(fake_perth) None None None None None None None None
f4dcbc02 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
5b9eeadc state StateTomography [Q0] DensityMatrix([[0.97558594+0.j        , 0.0332... unknown aer_simulator_from(fake_perth) None 1.0 [0.976779575624743, 0.02322042437525782] [0.976779575624743, 0.02322042437525782] False {'fitter': 'linear_inversion', 'fitter_time': ... 1.0 True
04916ee6 state_fidelity StateTomography [Q0] 0.975586 unknown aer_simulator_from(fake_perth) None None None None None None None None
0612d4a6 positive StateTomography [Q0] True unknown aer_simulator_from(fake_perth) None None None None None None None None

References

See also