Download Notebook

Example: Standard Quantum Measurement Process Tomography on 1-Qubit with Cvxpy

[1]:
import numpy as np

from quara.objects.qoperation_typical import (
    generate_qoperation,
    generate_qoperation_object,
)
from quara.objects.composite_system_typical import generate_composite_system
from quara.protocol.qtomography.standard.standard_qmpt import (
    StandardQmpt,
)
[2]:
# System
num_qubits = 1
c_sys = generate_composite_system(mode="qubit", num=num_qubits)
[3]:
# Tester Objects
state_names = ["x0", "y0", "z0", "z1"]
povm_names = ["x", "y", "z"]

tester_states = [
    generate_qoperation_object(
        mode="state", object_name="state", name=name, c_sys=c_sys
    )
        for name in state_names
]
tester_povms = [
    generate_qoperation_object(
        mode="povm", object_name="povm", name=name, c_sys=c_sys
    )
    for name in povm_names
]
[4]:
# True Object
true_object_name = "z-type2"
true_object = generate_qoperation(
    mode="mprocess", name=true_object_name, c_sys=c_sys
)
[5]:
# Qmpt
on_para_eq_constraint = True
qmpt = StandardQmpt(
    states=tester_states,
    povms=tester_povms,
    num_outcomes=true_object.num_outcomes,
    on_para_eq_constraint=on_para_eq_constraint,
    schedules="all",
)
[6]:
# empi_dists
prob_dists = qmpt.calc_prob_dists(true_object)
empi_dists = [(10, prob_dist) for prob_dist in prob_dists]

Qmpt with Cvxpy

[7]:
from quara.interface.cvxpy.qtomography.standard.loss_function import (
    CvxpyLossFunctionOption,
    CvxpyRelativeEntropy,
    CvxpyUniformSquaredError,
    CvxpyApproximateRelativeEntropyWithZeroProbabilityTerm,
)
from quara.interface.cvxpy.qtomography.standard.minimization_algorithm import (
    CvxpyMinimizationAlgorithm,
    CvxpyMinimizationAlgorithmOption,
)
from quara.interface.cvxpy.qtomography.standard.estimator import (
    CvxpyLossMinimizationEstimator,
)
[8]:
# Loss and Algorithm
mode_constraint = "physical"
#mode_constraint = "unconstraint"
name_solver = "mosek"
#name_solver = "scs"
#name_solver = "cvxopt"

loss = CvxpyRelativeEntropy()
#loss = CvxpyUniformSquaredError()
#loss = CvxpyApproximateRelativeEntropyWithZeroProbabilityTerm()
loss_option = CvxpyLossFunctionOption()
algo = CvxpyMinimizationAlgorithm()
algo_option = CvxpyMinimizationAlgorithmOption(
    name_solver=name_solver, mode_constraint=mode_constraint
)
estimator = CvxpyLossMinimizationEstimator()
[9]:
import numpy.testing as npt
[10]:
# Estimation
sqt = qmpt
result = estimator.calc_estimate(
    qtomography=sqt,
    empi_dists=empi_dists,
    loss=loss,
    loss_option=loss_option,
    algo=algo,
    algo_option=algo_option,
    is_computation_time_required=True,
)
var_estimate = result.estimated_var

# Test
actual = var_estimate
expected = true_object.to_var()

decimal = 1e-8
npt.assert_almost_equal(actual, expected, decimal=decimal)
print("\n")
print("mode_constraint =", mode_constraint)
print("actual          =", actual)
print("expected        =", expected)
print("squared_error   =", np.inner(actual-expected, actual-expected))


mode_constraint = physical
actual          = [ 5.00000000e-01  4.19112990e-11 -1.71018651e-11  5.00000000e-01
  1.82633930e-10 -1.19231459e-11 -4.12192314e-11  1.82634937e-10
 -9.05795422e-11  4.12194987e-11 -1.19250450e-11 -9.05806765e-11
  5.00000000e-01  8.86938019e-11  2.96807284e-11  5.00000000e-01
 -7.14831436e-12 -7.59382841e-12  2.37046817e-11  7.14824700e-12
 -5.05432244e-11  2.37044410e-11  7.59362887e-12  5.05433277e-11
  5.00000000e-01  4.87125442e-12  6.38844411e-11 -5.00000000e-01]
expected        = [ 0.5  0.   0.   0.5  0.   0.   0.   0.   0.   0.   0.   0.   0.5  0.
  0.   0.5  0.   0.   0.   0.   0.   0.   0.   0.   0.5  0.   0.  -0.5]
squared_error   = 5.490980077466456e-19

Download Notebook