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.47119141+0.j        ,  0.01416016-0.0164388j ,
                -0.00764974+0.00048828j, -0.00830078-0.44726562j],
               [ 0.01416016+0.0164388j ,  0.03597005+0.j        ,
                -0.01416016+0.00683594j,  0.0069987 -0.01318359j],
               [-0.00764974-0.00048828j, -0.01416016-0.00683594j,
                 0.0304362 +0.j        ,  0.00537109+0.00602214j],
               [-0.00830078+0.44726562j,  0.0069987 +0.01318359j,
                 0.00537109-0.00602214j,  0.46240234+0.j        ]],
              dims=(2, 2))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9140624999999993
- 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.47119141+0.j        ,  0.01416016-0.0164388j ,
                -0.00764974+0.00048828j, -0.00830078-0.44726562j],
               [ 0.01416016+0.0164388j ,  0.03597005+0.j        ,
                -0.01416016+0.00683594j,  0.0069987 -0.01318359j],
               [-0.00764974-0.00048828j, -0.01416016-0.00683594j,
                 0.0304362 +0.j        ,  0.00537109+0.00602214j],
               [-0.00830078+0.44726562j,  0.0069987 +0.01318359j,
                 0.00537109-0.00602214j,  0.46240234+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.91406

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.91502373, 0.0503988 , 0.01967458, 0.01490289]),
 'raw_eigvals': array([0.91502373, 0.0503988 , 0.01967458, 0.01490289]),
 'rescaled_psd': False,
 'fitter_metadata': {'fitter': 'linear_inversion',
  'fitter_time': 0.0036857128143310547},
 '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.51765076+0.00000000e+00j,  0.01140116-1.36386187e-02j,
                -0.08503205+2.77125502e-02j,  0.0464011 -3.99911442e-01j],
               [ 0.01140116+1.36386187e-02j,  0.03422156+0.00000000e+00j,
                -0.04290423+3.61333680e-02j,  0.01632976-1.56300293e-02j],
               [-0.08503205-2.77125502e-02j, -0.04290423-3.61333680e-02j,
                 0.12837554+6.93889390e-18j, -0.04923645+7.52310649e-02j],
               [ 0.0464011 +3.99911442e-01j,  0.01632976+1.56300293e-02j,
                -0.04923645-7.52310649e-02j,  0.31975215+0.00000000e+00j]],
              dims=(2, 2))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False
{'trace': 1.000000000000001,
 'eigvals': array([0.85629037, 0.13732086, 0.00638877, 0.        ]),
 'raw_eigvals': array([ 0.8908404 ,  0.17187089,  0.0409388 , -0.1036501 ]),
 'rescaled_psd': True,
 'fitter_metadata': {'fitter': 'linear_inversion',
  'fitter_time': 0.0027115345001220703},
 '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.47902208+0.j        ,  0.0030581 +0.00929345j,
                -0.00462908+0.00664701j,  0.00431976-0.44035749j],
               [ 0.0030581 -0.00929345j,  0.02674516+0.j        ,
                -0.01682347-0.00667329j,  0.00599744-0.00277336j],
               [-0.00462908-0.00664701j, -0.01682347+0.00667329j,
                 0.02685259+0.j        ,  0.00247863-0.01338204j],
               [ 0.00431976+0.44035749j,  0.00599744+0.00277336j,
                 0.00247863+0.01338204j,  0.46738017+0.j        ]],
              dims=(2, 2))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0', 'Q1']
- verified: False

extra:
- trace: 1.0000000040886086
- eigvals: [9.13696095e-01 4.95551863e-02 3.67257609e-02 2.29574761e-05]
- raw_eigvals: [9.13696092e-01 4.95551861e-02 3.67257608e-02 2.29574760e-05]
- rescaled_psd: False
- fitter_metadata: {'fitter': 'cvxpy_gaussian_lstsq', 'cvxpy_solver': 'SCS', 'cvxpy_status': ['optimal'], 'psd_constraint': True, 'trace_preserving': True, 'fitter_time': 0.20354652404785156}
- 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)
AnalysisResult
- name: state
- value: DensityMatrix([[0.97070312+0.j        , 0.00195312+0.01269531j],
               [0.00195312-0.01269531j, 0.02929688+0.j        ]],
              dims=(2,))
- quality: unknown
- extra: <9 items>
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9707031249999999
- quality: unknown
- extra: <9 items>
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: positive
- value: True
- quality: unknown
- extra: <9 items>
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: state
- value: DensityMatrix([[0.84570312+0.j      , 0.        +0.359375j],
               [0.        -0.359375j, 0.15429688+0.j      ]],
              dims=(2,))
- quality: unknown
- extra: <9 items>
- device_components: ['Q1']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9985655234537962
- quality: unknown
- extra: <9 items>
- device_components: ['Q1']
- verified: False
AnalysisResult
- name: positive
- value: True
- quality: unknown
- extra: <9 items>
- device_components: ['Q1']
- verified: False
AnalysisResult
- name: state
- value: DensityMatrix([[ 0.4921875 +0.j        , -0.02441406+0.46386719j],
               [-0.02441406-0.46386719j,  0.5078125 +0.j        ]],
              dims=(2,))
- quality: unknown
- extra: <9 items>
- device_components: ['Q2']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9638671875000002
- quality: unknown
- extra: <9 items>
- device_components: ['Q2']
- verified: False
AnalysisResult
- name: positive
- value: True
- quality: unknown
- extra: <9 items>
- device_components: ['Q2']
- verified: False
AnalysisResult
- name: state
- value: DensityMatrix([[0.16796875+0.j        , 0.00683594+0.33984375j],
               [0.00683594-0.33984375j, 0.83203125+0.j        ]],
              dims=(2,))
- quality: unknown
- extra: <9 items>
- device_components: ['Q3']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9750873686097118
- quality: unknown
- extra: <9 items>
- device_components: ['Q3']
- verified: False
AnalysisResult
- name: positive
- value: True
- quality: unknown
- extra: <9 items>
- device_components: ['Q3']
- verified: False
AnalysisResult
- name: state
- value: DensityMatrix([[0.03417969+0.j        , 0.01269531-0.03808594j],
               [0.01269531+0.03808594j, 0.96582031+0.j        ]],
              dims=(2,))
- quality: unknown
- extra: <9 items>
- device_components: ['Q4']
- verified: False
AnalysisResult
- name: state_fidelity
- value: 0.9658203125000003
- quality: unknown
- extra: <9 items>
- device_components: ['Q4']
- verified: False
AnalysisResult
- name: positive
- value: True
- quality: unknown
- extra: <9 items>
- device_components: ['Q4']
- verified: False

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

References

See also