Skip to content

Commit

Permalink
Update experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed May 6, 2021
1 parent 9bf7784 commit 6b8ed7d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 48 deletions.
49 changes: 33 additions & 16 deletions qiskit_experiments/characterization/t1_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from qiskit.circuit import QuantumCircuit
from qiskit.utils import apply_prefix
from qiskit.providers.options import Options

from qiskit_experiments.base_experiment import BaseExperiment
from qiskit_experiments.base_analysis import BaseAnalysis
Expand All @@ -27,9 +28,29 @@


class T1Analysis(BaseAnalysis):
"""T1 Experiment result analysis class."""
"""T1 Experiment result analysis class.
Analysis Options:
t1_guess (float): Optional, an initial guess of T1
amplitude_guess (float): Optional, an initial guess of the coefficient of the exponent
offset_guess (float): Optional, an initial guess of the offset
t1_bounds (list of two floats): Optional, lower bound and upper bound to T1
amplitude_bounds (list of two floats): Optional, lower bound and upper bound to the amplitude
offset_bounds (list of two floats): Optional, lower bound and upper bound to the offset
"""

@classmethod
def _default_options(cls):
return Options(
t1_guess=None,
amplitude_guess=None,
offset_guess=None,
t1_bounds=None,
amplitude_bounds=None,
offset_bounds=None,
)

# pylint: disable=arguments-differ, unused-argument
# pylint: disable=arguments-differ
def _run_analysis(
self,
experiment_data,
Expand All @@ -39,7 +60,6 @@ def _run_analysis(
t1_bounds=None,
amplitude_bounds=None,
offset_bounds=None,
**kwargs,
) -> Tuple[AnalysisResult, None]:
"""
Calculate T1
Expand All @@ -52,12 +72,10 @@ def _run_analysis(
t1_bounds (list of two floats): Optional, lower bound and upper bound to T1
amplitude_bounds (list of two floats): Optional, lower bound and upper bound to the amplitude
offset_bounds (list of two floats): Optional, lower bound and upper bound to the offset
kwargs: Trailing unused function parameters
Returns:
The analysis result with the estimated T1
"""

unit = experiment_data._data[0]["metadata"]["unit"]
conversion_factor = experiment_data._data[0]["metadata"].get("dt_factor", None)
if conversion_factor is None:
Expand Down Expand Up @@ -135,6 +153,10 @@ class T1Experiment(BaseExperiment):

__analysis_class__ = T1Analysis

@classmethod
def _default_options(cls) -> Options:
return Options(delays=None, unit="s")

def __init__(
self,
qubit: int,
Expand All @@ -154,12 +176,8 @@ def __init__(
"""
if len(delays) < 3:
raise ValueError("T1 experiment: number of delays must be at least 3")
super().__init__([qubit], delays=delays, unit=unit)

self._delays = delays
self._unit = unit
super().__init__([qubit])

# pylint: disable=arguments-differ
def circuits(self, backend: Optional["Backend"] = None) -> List[QuantumCircuit]:
"""
Return a list of experiment circuits
Expand All @@ -173,31 +191,30 @@ def circuits(self, backend: Optional["Backend"] = None) -> List[QuantumCircuit]:
Raises:
AttributeError: if unit is dt but dt parameter is missing in the backend configuration
"""

if self._unit == "dt":
if self.options.unit == "dt":
try:
dt_factor = getattr(backend.configuration(), "dt")
except AttributeError as no_dt:
raise AttributeError("Dt parameter is missing in backend configuration") from no_dt

circuits = []

for delay in self._delays:
for delay in self.options.delays:
circ = QuantumCircuit(1, 1)
circ.x(0)
circ.barrier(0)
circ.delay(delay, 0, self._unit)
circ.delay(delay, 0, self.options.unit)
circ.barrier(0)
circ.measure(0, 0)

circ.metadata = {
"experiment_type": self._type,
"qubit": self.physical_qubits[0],
"xval": delay,
"unit": self._unit,
"unit": self.options.unit,
}

if self._unit == "dt":
if self.options.unit == "dt":
circ.metadata["dt_factor"] = dt_factor

circuits.append(circ)
Expand Down
4 changes: 2 additions & 2 deletions qiskit_experiments/composite/batch_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, experiments):
qubits = tuple(self._qubit_map.keys())
super().__init__(experiments, qubits)

def circuits(self, backend=None, **circuit_options):
def circuits(self, backend=None):

batch_circuits = []

Expand All @@ -51,7 +51,7 @@ def circuits(self, backend=None, **circuit_options):
qubit_mapping = None
else:
qubit_mapping = [self._qubit_map[qubit] for qubit in expr.physical_qubits]
for circuit in expr.circuits(**circuit_options):
for circuit in expr.circuits(backend):
# Update metadata
circuit.metadata = {
"experiment_type": self._type,
Expand Down
2 changes: 1 addition & 1 deletion qiskit_experiments/composite/composite_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _run_analysis(self, experiment_data, **options):
for expr, expr_data in zip(
experiment_data._experiment._experiments, experiment_data._composite_expdata
):
expr.analysis().run(expr_data, **options)
expr.analysis(**expr.analysis_options.__dict__).run(expr_data, **options)

# Add sub-experiment metadata as result of batch experiment
# Note: if Analysis results had ID's these should be included here
Expand Down
13 changes: 5 additions & 8 deletions qiskit_experiments/composite/composite_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,21 @@ class CompositeExperiment(BaseExperiment):
__analysis_class__ = CompositeAnalysis
__experiment_data__ = CompositeExperimentData

def __init__(self, experiments, qubits, experiment_type=None, circuit_options=None):
def __init__(self, experiments, qubits, experiment_type=None):
"""Initialize the composite experiment object.
Args:
experiments (List[BaseExperiment]): a list of experiment objects.
qubits (int or Iterable[int]): the number of qubits or list of
physical qubits for the experiment.
experiment_type (str): Optional, composite experiment subclass name.
circuit_options (str): Optional, Optional, dictionary of allowed
kwargs and default values for the `circuit`
method.
"""
self._experiments = experiments
self._num_experiments = len(experiments)
super().__init__(qubits, experiment_type=experiment_type, circuit_options=circuit_options)
super().__init__(qubits, experiment_type=experiment_type)

@abstractmethod
def circuits(self, backend=None, **circuit_options):
def circuits(self, backend=None):
pass

@property
Expand All @@ -55,6 +52,6 @@ def component_experiment(self, index):
"""Return the component Experiment object"""
return self._experiments[index]

def component_analysis(self, index, **kwargs):
def component_analysis(self, index, **analysis_options):
"""Return the component experiment Analysis object"""
return self.component_experiment(index).analysis(**kwargs)
return self.component_experiment(index).analysis(**analysis_options)
4 changes: 2 additions & 2 deletions qiskit_experiments/composite/parallel_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, experiments):
qubits += exp.physical_qubits
super().__init__(experiments, qubits)

def circuits(self, backend=None, **circuit_options):
def circuits(self, backend=None):

sub_circuits = []
sub_qubits = []
Expand All @@ -42,7 +42,7 @@ def circuits(self, backend=None, **circuit_options):
# Generate data for combination
for expr in self._experiments:
# Add subcircuits
circs = expr.circuits(**circuit_options)
circs = expr.circuits(backend)
sub_circuits.append(circs)
sub_size.append(len(circs))

Expand Down
18 changes: 17 additions & 1 deletion qiskit_experiments/randomized_benchmarking/rb_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"""

from typing import Optional, List
import numpy as np

from qiskit.providers.options import Options
from qiskit_experiments.base_analysis import BaseAnalysis
from qiskit_experiments.analysis.curve_fitting import curve_fit, process_curve_data
from qiskit_experiments.analysis.data_processing import (
Expand All @@ -32,7 +34,21 @@


class RBAnalysis(BaseAnalysis):
"""RB Analysis class."""
"""RB Analysis class.
Analysis Options:
p0: Optional, initial parameter values for curve_fit.
plot: If True generate a plot of fitted data.
ax: Optional, matplotlib axis to add plot to.
"""

@classmethod
def _default_options(cls):
return Options(
p0=None,
plot=True,
ax=None,
)

# pylint: disable = arguments-differ, invalid-name
def _run_analysis(
Expand Down
17 changes: 0 additions & 17 deletions qiskit_experiments/randomized_benchmarking/rb_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,6 @@ def circuits(self, backend=None):
circuits += self._sample_circuits(self._lengths, seed=self._rng)
return circuits

def transpiled_circuits(self, backend=None, **kwargs):
"""Return a list of transpiled RB circuits.
Args:
backend (Backend): Optional, a backend object to use as the
argument for the :func:`qiskit.transpile`
function.
kwargs: kwarg options for the :func:`qiskit.transpile` function.
Returns:
List[QuantumCircuit]: A list of :class:`QuantumCircuit`s.
Raises:
QiskitError: if an initial layout is specified in the
kwarg options for transpilation. The initial
layout must be generated from the experiment.
"""
circuits = super().transpiled_circuits(backend=backend, **kwargs)
return circuits

def _sample_circuits(
self, lengths: Iterable[int], seed: Optional[Union[int, Generator]] = None
):
Expand Down
2 changes: 1 addition & 1 deletion test/data_processing/test_data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self):
self._type = None
super().__init__((0,), "fake_test_experiment")

def circuits(self, backend=None, **circuit_options):
def circuits(self, backend=None):
"""Fake circuits."""
return []

Expand Down

0 comments on commit 6b8ed7d

Please sign in to comment.