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

Bugfix for Germ Selection Line Labels and Instrument Specifier Type Error Bug #508

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
31 changes: 26 additions & 5 deletions pygsti/algorithms/germselection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pygsti.baseobjs.statespace import ExplicitStateSpace as _ExplicitStateSpace
from pygsti.baseobjs.statespace import QuditSpace as _QuditSpace
from pygsti.models import ExplicitOpModel as _ExplicitOpModel
from pygsti.forwardsims import MatrixForwardSimulator as _MatrixForwardSimulator

FLOATSIZE = 8 # in bytes: TODO: a better way

Expand Down Expand Up @@ -57,10 +58,8 @@ def find_germs(target_model, randomize=True, randomization_strength=1e-2,

Parameters
----------
target_model : Model or list of Model
The model you are aiming to implement, or a list of models that are
copies of the model you are trying to implement (either with or
without random unitary perturbations applied to the models).
target_model : Model
The model you are aiming to implement.

randomize : bool, optional
Whether or not to add random unitary perturbations to the model(s)
Expand Down Expand Up @@ -188,8 +187,14 @@ def find_germs(target_model, randomize=True, randomization_strength=1e-2,
A list containing the germs making up the germ set.
"""
printer = _baseobjs.VerbosityPrinter.create_printer(verbosity, comm)

if not isinstance(target_model.sim, _MatrixForwardSimulator):
target_model = target_model.copy()
target_model.sim = 'matrix'

modelList = _setup_model_list(target_model, randomize,
randomization_strength, num_gs_copies, seed)

gates = list(target_model.operations.keys())
availableGermsList = []
if candidate_germ_counts is None: candidate_germ_counts = {6: 'all upto'}
Expand Down Expand Up @@ -401,7 +406,19 @@ def find_germs(target_model, randomize=True, randomization_strength=1e-2,
raise ValueError("'{}' is not a valid algorithm "
"identifier.".format(algorithm))

return germList
#force the line labels on each circuit to match the state space labels for the target model.
#this is suboptimal for many-qubit models, so will probably want to revisit this. #TODO
finalGermList = []
for ckt in germList:
if ckt._static:
new_ckt = ckt.copy(editable=True)
new_ckt.line_labels = target_model.state_space.state_space_labels
new_ckt.done_editing()
finalGermList.append(new_ckt)
else:
ckt.line_labels = target_model.state_space.state_space_labels
finalGermList.append(ckt)
return finalGermList


def compute_germ_set_score(germs, target_model=None, neighborhood=None,
Expand Down Expand Up @@ -1351,6 +1368,10 @@ def test_germ_set_finitel(model, germs_to_test, length, weights=None,
eigenvalues (from small to large) of the jacobian^T * jacobian
matrix used to determine parameter amplification.
"""
if not isinstance(model.sim, _MatrixForwardSimulator):
model = model.copy()
model.sim = 'matrix'

# Remove any SPAM vectors from model since we only want
# to consider the set of *gate* parameters for amplification
# and this makes sure our parameter counting is correct
Expand Down
4 changes: 1 addition & 3 deletions pygsti/forwardsims/forwardsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def cast(cls, obj : ForwardSimulator.Castable, num_qubits=None):
if isinstance(obj, ForwardSimulator):
return obj
elif isinstance(obj, str):
if obj == "auto":
return _MapFSim() if (num_qubits is None or num_qubits > 2) else _MatrixFSim()
elif obj == "map":
if obj == "auto" or obj == "map":
return _MapFSim()
elif obj == "matrix":
return _MatrixFSim()
Expand Down
1 change: 0 additions & 1 deletion pygsti/forwardsims/mapforwardsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def __getstate__(self):
# and this is done by the parent model which will cause _set_evotype to be called.
return state


class MapForwardSimulator(_DistributableForwardSimulator, SimpleMapForwardSimulator):
"""
Computes circuit outcome probabilities using circuit layer maps that act on a state.
Expand Down
139 changes: 131 additions & 8 deletions pygsti/modelmembers/povms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# in compliance with the License. You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0 or in the LICENSE file in the root pyGSTi directory.
#***************************************************************************************************
import _collections
import functools as _functools
import itertools as _itertools

Expand Down Expand Up @@ -42,10 +41,40 @@

def create_from_pure_vectors(pure_vectors, povm_type, basis='pp', evotype='default', state_space=None,
on_construction_error='warn'):
""" TODO: docstring -- create a POVM from a list/dict of (key, pure-vector) pairs """
"""
Creates a Positive Operator-Valued Measure (POVM) from a list or dictionary of (key, pure-vector) pairs.

Parameters
----------
pure_vectors : list or dict
A list of (key, pure-vector) pairs or a dictionary where keys are labels and values are pure state vectors.

povm_type : str or tuple
The type of POVM to create. This can be a single string or a tuple of strings indicating the preferred types.
Supported types include 'computational', 'static pure', 'full pure', 'static', 'full', 'full TP', and any valid
Lindblad parameterization type.

basis : str, optional
The basis in which the pure vectors are expressed. Default is 'pp'.

evotype : str, optional
The evolution type. Default is 'default'.

state_space : StateSpace, optional
The state space in which the POVM operates. Default is None.

on_construction_error : str, optional
Specifies the behavior when an error occurs during POVM construction. Options are 'raise' to raise the error,
'warn' to print a warning message, or any other value to silently ignore the error. Default is 'warn'.

Returns
-------
POVM
The constructed POVM object.
"""
povm_type_preferences = (povm_type,) if isinstance(povm_type, str) else povm_type
if not isinstance(pure_vectors, dict): # then assume it's a list of (key, value) pairs
pure_vectors = _collections.OrderedDict(pure_vectors)
pure_vectors = dict(pure_vectors)
if state_space is None:
state_space = _statespace.default_space_for_udim(len(next(iter(pure_vectors.values()))))

Expand Down Expand Up @@ -94,10 +123,41 @@ def create_from_pure_vectors(pure_vectors, povm_type, basis='pp', evotype='defau

def create_from_dmvecs(superket_vectors, povm_type, basis='pp', evotype='default', state_space=None,
on_construction_error='warn'):
""" TODO: docstring -- create a POVM from a list/dict of (key, pure-vector) pairs """
"""
Creates a Positive Operator-Valued Measure (POVM) from a list or dictionary of (key, superket) pairs.

Parameters
----------
superket_vectors : list or dict
A list of (key, pure-vector) pairs or a dictionary where keys are labels and values are superket vectors.
i.e. vectorized density matrices.

povm_type : str or tuple
The type of POVM to create. This can be a single string or a tuple of strings indicating the preferred types.
Supported types include 'full', 'static', 'full TP', 'computational', 'static pure', 'full pure', and any valid
Lindblad parameterization type.

basis : str or `Basis`, optional
The basis in which the density matrix vectors are expressed. Default is 'pp'.

evotype : str, optional
The evolution type. Default is 'default'.

state_space : StateSpace, optional
The state space in which the POVM operates. Default is None.

on_construction_error : str, optional
Specifies the behavior when an error occurs during POVM construction. Options are 'raise' to raise the error,
'warn' to print a warning message, or any other value to silently ignore the error. Default is 'warn'.

Returns
-------
POVM
The constructed POVM object.
"""
povm_type_preferences = (povm_type,) if isinstance(povm_type, str) else povm_type
if not isinstance(superket_vectors, dict): # then assume it's a list of (key, value) pairs
superket_vectors = _collections.OrderedDict(superket_vectors)
superket_vectors = dict(superket_vectors)

for typ in povm_type_preferences:
try:
Expand Down Expand Up @@ -140,12 +200,42 @@ def create_from_dmvecs(superket_vectors, povm_type, basis='pp', evotype='default
print('Failed to construct povm with type "{}" with error: {}'.format(typ, str(err)))
pass # move on to next type

raise ValueError("Could not create a POVM of type(s) %s from the given pure vectors!" % (str(povm_type)))
raise ValueError("Could not create a POVM of type(s) %s from the given density matrix vectors!" % (str(povm_type)))


def create_effect_from_pure_vector(pure_vector, effect_type, basis='pp', evotype='default', state_space=None,
on_construction_error='warn'):
""" TODO: docstring -- create a State from a state vector """
"""
Creates a POVM effect from a pure state vector.

Parameters
----------
pure_vector : array-like
The pure state vector from which to create the POVM effect.

effect_type : str or tuple
The type of effect to create. This can be a single string or a tuple of strings indicating the preferred types.
Supported types include 'computational', 'static pure', 'full pure', 'static', 'full', 'static clifford', and
any valid Lindblad parameterization type.

basis : str or `Basis` optional
The basis in which the pure vector is expressed. Default is 'pp'.

evotype : str, optional
The evolution type. Default is 'default'.

state_space : StateSpace, optional
The state space in which the effect operates. Default is None.

on_construction_error : str, optional
Specifies the behavior when an error occurs during effect construction. Options are 'raise' to raise the error,
'warn' to print a warning message, or any other value to silently ignore the error. Default is 'warn'.

Returns
-------
POVMEffect
The constructed POVM effect object.
"""
effect_type_preferences = (effect_type,) if isinstance(effect_type, str) else effect_type
if state_space is None:
state_space = _statespace.default_space_for_udim(len(pure_vector))
Expand Down Expand Up @@ -196,6 +286,38 @@ def create_effect_from_pure_vector(pure_vector, effect_type, basis='pp', evotype

def create_effect_from_dmvec(superket_vector, effect_type, basis='pp', evotype='default', state_space=None,
on_construction_error='warn'):
"""
Creates a POVM effect from a density matrix vector (superket).

Parameters
----------
superket_vector : array-like
The density matrix vector (superket) from which to create the POVM effect.

effect_type : str or tuple
The type of effect to create. This can be a single string or a tuple of strings indicating the preferred types.
Supported types include 'static', 'full', and any valid Lindblad parameterization type. For other types
we first try to convert to a pure state vector and then utilize `create_effect_from_pure_vector`

basis : str or `Basis` optional
The basis in which the superket vector is expressed. Default is 'pp'.

evotype : str, optional
The evolution type. Default is 'default'.

state_space : StateSpace, optional
The state space in which the effect operates. Default is None.

on_construction_error : str, optional
Specifies the behavior when an error occurs during effect construction. Options are 'raise' to raise the error,
'warn' to print a warning message, or any other value to silently ignore the error. Default is 'warn'.

Returns
-------
POVMEffect
The constructed POVM effect object.
"""

effect_type_preferences = (effect_type,) if isinstance(effect_type, str) else effect_type
if state_space is None:
state_space = _statespace.default_space_for_dim(len(superket_vector))
Expand Down Expand Up @@ -241,7 +363,8 @@ def create_effect_from_dmvec(superket_vector, effect_type, basis='pp', evotype='


def povm_type_from_op_type(op_type):
"""Decode an op type into an appropriate povm type.
"""
Decode an op type into an appropriate povm type.

Parameters:
-----------
Expand Down
Loading
Loading