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
for result in qstdata1.analysis_results():
    print(result)
AnalysisResult
- name: state
- value: DensityMatrix([[ 0.47086589+0.j        , -0.00472005+0.00537109j,
                 0.0008138 -0.01383464j, -0.00585938-0.45556641j],
               [-0.00472005-0.00537109j,  0.01708984+0.j        ,
                 0.        +0.00439453j, -0.00406901+0.00374349j],
               [ 0.0008138 +0.01383464j,  0.        -0.00439453j,
                 0.02685547+0.j        ,  0.00211589-0.00244141j],
               [-0.00585938+0.45556641j, -0.00406901-0.00374349j,
                 0.00211589+0.00244141j,  0.4851888 +0.j        ]],
              dims=(2, 2))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9335937499999996
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False
AnalysisResult
- name: positive
- value: True
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False

Tomography Results

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

state_result = qstdata1.analysis_results("state")
print(state_result.value)
DensityMatrix([[ 0.47086589+0.j        , -0.00472005+0.00537109j,
                 0.0008138 -0.01383464j, -0.00585938-0.45556641j],
               [-0.00472005-0.00537109j,  0.01708984+0.j        ,
                 0.        +0.00439453j, -0.00406901+0.00374349j],
               [ 0.0008138 +0.01383464j,  0.        -0.00439453j,
                 0.02685547+0.j        ,  0.00211589-0.00244141j],
               [-0.00585938+0.45556641j, -0.00406901-0.00374349j,
                 0.00211589+0.00244141j,  0.4851888 +0.j        ]],
              dims=(2, 2))

We can also visualize the density matrix:

from qiskit.visualization import plot_state_city
plot_state_city(qstdata1.analysis_results("state").value, 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")
print("State Fidelity = {:.5f}".format(fid_result.value))
State Fidelity = 0.93359

Additional state metadata

Additional data is stored in the tomography under the "state_metadata" field. This includes

  • eigvals: the eigenvalues of the fitted state

  • trace: the trace of the fitted state

  • positive: Whether the eigenvalues are all non-negative

  • positive_delta: the deviation from positivity given by 1-norm of negative eigenvalues.

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.

state_result.extra
{'trace': 1.0000000000000016,
 'eigvals': array([0.93392072, 0.03418321, 0.01797588, 0.01392019]),
 'raw_eigvals': array([0.93392072, 0.03418321, 0.01797588, 0.01392019]),
 'rescaled_psd': False,
 'fitter_metadata': {'fitter': 'linear_inversion',
  'fitter_time': 0.00816655158996582},
 'conditional_probability': 1.0,
 'positive': True,
 'experiment': 'StateTomography',
 'run_time': None}

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")

# Print result
print(bad_state_result)

# Show extra data
bad_state_result.extra
AnalysisResult
- name: state
- value: DensityMatrix([[ 0.55837202+0.00000000e+00j, -0.016519  -5.27958177e-03j,
                -0.08570984+6.84406963e-02j, -0.03327265-4.07226526e-01j],
               [-0.016519  +5.27958177e-03j,  0.00317856-2.16840434e-19j,
                -0.00784581+6.49831452e-03j,  0.01339307+1.92298081e-02j],
               [-0.08570984-6.84406963e-02j, -0.00784581-6.49831452e-03j,
                 0.09043768-8.67361738e-19j, -0.04985901+8.68587861e-03j],
               [-0.03327265+4.07226526e-01j,  0.01339307-1.92298081e-02j,
                -0.04985901-8.68587861e-03j,  0.34801174+3.46944695e-18j]],
              dims=(2, 2))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False
{'trace': 0.9999999999999999,
 'eigvals': array([0.89118558, 0.10881442, 0.        , 0.        ]),
 'raw_eigvals': array([ 0.9831729 ,  0.20080174,  0.0198502 , -0.20382484]),
 'rescaled_psd': True,
 'fitter_metadata': {'fitter': 'linear_inversion',
  'fitter_time': 0.004689693450927734},
 'conditional_probability': 1.0,
 'positive': True,
 'experiment': 'StateTomography',
 'run_time': None}

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")
    print(state_result2)
    print("\nextra:")
    for key, val in state_result2.extra.items():
        print(f"- {key}: {val}")

except ModuleNotFoundError:
    print("CVXPY is not installed")
AnalysisResult
- name: state
- value: DensityMatrix([[ 0.47742658+0.j        ,  0.01164514-0.00151531j,
                 0.0046994 -0.02068738j, -0.0280339 -0.4481473j ],
               [ 0.01164514+0.00151531j,  0.02096209+0.j        ,
                -0.00176451+0.0040383j ,  0.00106344+0.00806419j],
               [ 0.0046994 +0.02068738j, -0.00176451-0.0040383j ,
                 0.02843095+0.j        , -0.00587117-0.00255456j],
               [-0.0280339 +0.4481473j ,  0.00106344-0.00806419j,
                -0.00587117+0.00255456j,  0.47318037+0.j        ]],
              dims=(2, 2))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False

extra:
- trace: 0.999999999997993
- eigvals: [9.24499780e-01 4.79093684e-02 2.75908165e-02 3.54782765e-08]
- raw_eigvals: [9.24499780e-01 4.79093684e-02 2.75908165e-02 3.54782765e-08]
- rescaled_psd: False
- fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.021882295608520508}
- conditional_probability: 1.0
- positive: True
- experiment: StateTomography
- run_time: None

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()

for result in pardata.analysis_results():
    print(result)

View component experiment analysis results:

for i, expdata in enumerate(pardata.child_data()):
    state_result_i = expdata.analysis_results("state")
    fid_result_i = expdata.analysis_results("state_fidelity")

    print(f'\nPARALLEL EXP {i}')
    print("State Fidelity: {:.5f}".format(fid_result_i.value))
    print("State: {}".format(state_result_i.value))

PARALLEL EXP 0
State Fidelity: 0.98145
State: DensityMatrix([[ 0.98144531+0.j        , -0.00390625-0.00097656j],
               [-0.00390625+0.00097656j,  0.01855469+0.j        ]],
              dims=(2,))

PARALLEL EXP 1
State Fidelity: 0.97785
State: DensityMatrix([[0.84277344+0.j        , 0.00292969+0.33300781j],
               [0.00292969-0.33300781j, 0.15722656+0.j        ]],
              dims=(2,))

PARALLEL EXP 2
State Fidelity: 0.98438
State: DensityMatrix([[0.50488281+0.j      , 0.01757812+0.484375j],
               [0.01757812-0.484375j, 0.49511719+0.j      ]],
              dims=(2,))

PARALLEL EXP 3
State Fidelity: 0.99997
State: DensityMatrix([[ 0.14646973+0.00000000e+00j, -0.00571747+3.53530275e-01j],
               [-0.00571747-3.53530275e-01j,  0.85353027+1.73472348e-18j]],
              dims=(2,))

PARALLEL EXP 4
State Fidelity: 0.98535
State: DensityMatrix([[0.01464844+0.j        , 0.01074219+0.00683594j],
               [0.01074219-0.00683594j, 0.98535156+0.j        ]],
              dims=(2,))

References

See also