Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds target kwarg to tomography init #1025

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion qiskit_experiments/library/tomography/qpt_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
basis_indices: Optional[Iterable[Tuple[List[int], List[int]]]] = None,
qubits: Optional[Sequence[int]] = None,
analysis: Union[BaseAnalysis, None, str] = "default",
target: Union[Statevector, DensityMatrix, None, str] = "default",
):
"""Initialize a quantum process tomography experiment.

Expand Down Expand Up @@ -94,6 +95,10 @@ def __init__(
analysis: Optional, a custom analysis instance to use. If ``"default"``
:class:`~.ProcessTomographyAnalysis` will be used. If None no analysis
instance will be set.
target: Optional, a custom quantum state target for computing the
state fidelity of the fitted density matrix during analysis.
If "default" the state will be inferred from the input circuit
if it contains no classical instructions.
"""
if analysis == "default":
analysis = ProcessTomographyAnalysis()
Expand All @@ -115,7 +120,9 @@ def __init__(

# Set target quantum channel
if isinstance(self.analysis, TomographyAnalysis):
self.analysis.set_options(target=self._target_quantum_channel())
if target == "default":
target = self._target_quantum_channel()
self.analysis.set_options(target=target)

def _target_quantum_channel(self) -> Union[Choi, Operator]:
"""Return the process tomography target"""
Expand Down
9 changes: 8 additions & 1 deletion qiskit_experiments/library/tomography/qst_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
basis_indices: Optional[Iterable[List[int]]] = None,
qubits: Optional[Sequence[int]] = None,
analysis: Union[BaseAnalysis, None, str] = "default",
target: Union[Statevector, DensityMatrix, None, str] = "default",
):
"""Initialize a quantum process tomography experiment.

Expand All @@ -82,6 +83,10 @@ def __init__(
analysis: Optional, a custom analysis instance to use. If ``"default"``
:class:`~.StateTomographyAnalysis` will be used. If None no analysis
instance will be set.
target: Optional, a custom quantum state target for computing the
state fidelity of the fitted density matrix during analysis.
If "default" the state will be inferred from the input circuit
if it contains no classical instructions.
"""
if isinstance(circuit, Statevector):
# Convert to circuit using initialize instruction
Expand Down Expand Up @@ -110,7 +115,9 @@ def __init__(

# Set target quantum state
if isinstance(self.analysis, TomographyAnalysis):
self.analysis.set_options(target=self._target_quantum_state())
if target == "default":
target = self._target_quantum_state()
self.analysis.set_options(target=target)

def _target_quantum_state(self) -> Union[Statevector, DensityMatrix]:
"""Return the state tomography target"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
---
features:
- |
Adds ``backend`` and ``analysis`` init kwargs to :class:`~.StateTomography`
and :class:`~.ProcessTomography` experiments to match the base
:class:`~.TomographyExperiment` init kwargs. This allows specifying the
intented backend, or a custom analysis class when initializing the
experiments.
Adds ``backend``, ``analysis``, and ``target`` init kwargs to the
:class:`~.StateTomography` and :class:`~.ProcessTomography` experiments.
These allow specifying intended backend, a custom analysis class, or a
custom target for fidelity calculations, when initializing the experiments.
upgrade:
- |
Renames the ``qubits``, ``measurement_qubits``, and ``preparation_qubits``
Expand Down
19 changes: 19 additions & 0 deletions test/library/tomography/test_process_tomography.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from qiskit_aer import AerSimulator
from qiskit_experiments.library import ProcessTomography
from qiskit_experiments.library.tomography import ProcessTomographyAnalysis
from qiskit_experiments.database_service import ExperimentEntryNotFound
from .tomo_utils import FITTERS, filter_results, teleport_circuit, teleport_bell_circuit


Expand Down Expand Up @@ -335,3 +336,21 @@ def test_expdata_serialization(self):
self.assertExperimentDone(expdata)
self.assertRoundTripPickle(expdata, check_func=self.experiment_data_equiv)
self.assertRoundTripSerializable(expdata, check_func=self.experiment_data_equiv)

def test_target_none(self):
"""Test setting target=None disables fidelity calculation."""
seed = 4343
backend = AerSimulator(seed_simulator=seed)
target = qi.random_unitary(2, seed=seed)
exp = ProcessTomography(target, backend=backend, target=None)
expdata = exp.run()
self.assertExperimentDone(expdata)
state = expdata.analysis_results("state").value
self.assertTrue(
isinstance(state, qi.Choi),
msg="Fitted state is not Choi matrix",
)
with self.assertRaises(
ExperimentEntryNotFound, msg="process_fidelity should not exist when target=None"
):
expdata.analysis_results("process_fidelity")
21 changes: 20 additions & 1 deletion test/library/tomography/test_state_tomography.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from qiskit_experiments.library import StateTomography
from qiskit_experiments.library.tomography import StateTomographyAnalysis
from qiskit_experiments.database_service import ExperimentEntryNotFound
from .tomo_utils import FITTERS, filter_results, teleport_circuit, teleport_bell_circuit


Expand Down Expand Up @@ -243,8 +244,26 @@ def test_experiment_config(self):
self.assertTrue(self.json_equiv(exp, loaded_exp))

def test_analysis_config(self):
""" "Test converting analysis to and from config works"""
"""Test converting analysis to and from config works"""
analysis = StateTomographyAnalysis()
loaded = StateTomographyAnalysis.from_config(analysis.config())
self.assertNotEqual(analysis, loaded)
self.assertEqual(analysis.config(), loaded.config())

def test_target_none(self):
"""Test setting target=None disables fidelity calculation."""
seed = 4343
backend = AerSimulator(seed_simulator=seed)
target = qi.random_statevector(2, seed=seed)
exp = StateTomography(target, backend=backend, target=None)
expdata = exp.run()
self.assertExperimentDone(expdata)
state = expdata.analysis_results("state").value
self.assertTrue(
isinstance(state, qi.DensityMatrix),
msg="Fitted state is not density matrix",
)
with self.assertRaises(
ExperimentEntryNotFound, msg="state_fidelity should not exist when target=None"
):
expdata.analysis_results("state_fidelity")