pygsti.protocols.gst.StandardGST#
- class StandardGST(modes=('full TP', 'CPTPLND', 'Target'), gaugeopt_suite='stdgaugeopt', target_model=None, models_to_test=None, objfn_builders=None, optimizer=None, badfit_options=None, verbosity=2, name=None)#
Bases:
ProtocolThe standard-practice GST protocol.
- Parameters:
modes (str, optional) –
A comma-separated list of modes which dictate what types of analyses are performed. Currently, these correspond to different types of parameterizations/constraints to apply to the estimated model. The default value is usually fine. Allowed values are:
”full” : full (completely unconstrained)
”TP” : TP-constrained
”CPTPLND” : Lindbladian CPTP-constrained
”H+S” : Only Hamiltonian + Stochastic errors allowed (CPTP)
”S” : Only Stochastic errors allowed (CPTP)
”Target” : use the target (ideal) gates as the estimate
<model> : any key in the models_to_test argument
gaugeopt_suite (GSTGaugeOptSuite, optional) – Specifies which gauge optimizations to perform on each estimate. Can also be any object that can be cast to a
GSTGaugeOptSuiteobject, such as a string or list of strings (see below) specifying built-in sets of gauge optimizations. This object also optionally stores an alternate target model for gauge optimization. This model is used as the “target” for gauge- optimization (only), and is useful when you want to gauge optimize toward something other than the ideal target gates.target_model (Model, optional (default None)) – If specified use this Model as the target model. Depending on other specified keyword arguments this model may be used as the target for the purposes of gauge optimization, report generation/analysis, and initial seeding for optimization. (For almost all of these it may be the case that other keyword argument values override this for certain tasks).
models_to_test (dict, optional) – A dictionary of Model objects representing (gate-set) models to test against the data. These Models are essentially hypotheses for which (if any) model generated the data. The keys of this dictionary can (and must, to actually test the models) be used within the comma- separate list given by the modes argument.
objfn_builders (GSTObjFnBuilders, optional) – The objective function(s) to optimize. Can also be anything that can be cast to a
GSTObjFnBuildersobject. Applies to all modes.optimizer (Optimizer, optional) – The optimizer to use. Can also be anything that can be case to a
Optimizer. Applies to all modes.badfit_options (GSTBadFitOptions, optional) – Options specifying what post-processing actions should be performed if the GST fit is unsatisfactory. Can also be anything that can be cast to a
GSTBadFitOptionsobject. Applies to all modes.verbosity (int, optional) – The ‘verbosity’ option is an integer specifying the level of detail printed to stdout during the calculation.
name (str, optional) – The name of this protocol, also used to (by default) name the results produced by this protocol. If None, the class name will be used.
Create a new Protocol object.
- Parameters:
name (str, optional) – The name of this protocol, also used to (by default) name the results produced by this protocol. If None, the class name will be used.
- Return type:
Methods
__init__([modes, gaugeopt_suite, ...])Create a new Protocol object.
add_mongodb_write_ops(write_ops, mongodb[, ...])Accumulate write and update operations for writing this object to a MongoDB database.
from_dir(dirname[, quick_load])Initialize a new Protocol object from dirname.
from_mongodb(mongodb, doc_id, **kwargs)Create and initialize an object from a MongoDB instance.
from_mongodb_doc(mongodb, collection_name, ...)Create and initialize an object from a MongoDB instance and pre-loaded primary document.
remove_from_mongodb(mongodb, doc_id[, ...])Remove the documents corresponding to an instance of this class from a MongoDB database.
remove_me_from_mongodb(mongodb[, session, ...])run(data[, memlimit, comm, checkpoint, ...])Run this protocol on data.
run_mpi(data, num_ranks, *[, mpiexec, ...])Run this protocol in parallel using MPI workers launched as a subprocess.
setup_nameddict(final_dict)Initializes a set of nested
NamedDictdictionaries describing this protocol.stage_slurm(data, num_ranks, slurm, work_dir, *)Write all working files to
work_dirand generate a SLURM batch script ready forsbatchsubmission.write(dirname)Write this protocol to a directory.
write_to_mongodb(mongodb[, session, ...])Write this object to a MongoDB database.
Attributes
collection_name- add_mongodb_write_ops(write_ops, mongodb, overwrite_existing=False, **kwargs)#
Accumulate write and update operations for writing this object to a MongoDB database.
Similar to
write_to_mongodb()but collects write operations instead of actually executing any write operations on the database. This function may be preferred towrite_to_mongodb()when this object is being written as a part of a larger entity and executing write operations is saved until the end.As in
write_to_mongodb(), self.collection_name is the collection name and _id is either: 1) the ID used by a previous write or initial read-in, if one exists, OR 2) a new random bson.objectid.ObjectId- Parameters:
write_ops (WriteOpsByCollection) – An object that keeps track of pymongo write operations on a per-collection basis. This object accumulates write operations to be performed at some point in the future.
mongodb (pymongo.database.Database) – The MongoDB instance to write data to.
overwrite_existing (bool, optional) – Whether existing documents should be overwritten. The default of False causes a ValueError to be raised if a document with the given _id already exists and is different from what is being written.
**kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _add_auxiliary_write_ops_and_update_doc method is allowed here.
- Returns:
The identifier (_id value) of the main document that was written.
- Return type:
bson.objectid.ObjectId
- classmethod from_dir(dirname, quick_load=False)#
Initialize a new Protocol object from dirname.
- quick_loadbool, optional
Setting this to True skips the loading of components that may take a long time to load.
- Parameters:
dirname (str) – The directory name.
quick_load (bool, optional) – Setting this to True skips the loading of components that may take a long time to load.
- Return type:
- classmethod from_mongodb(mongodb, doc_id, **kwargs)#
Create and initialize an object from a MongoDB instance.
- Parameters:
mongodb (pymongo.database.Database) – The MongoDB instance to load from.
doc_id (bson.objecctid.ObjectId or dict) – The object ID or filter used to find a single object ID within the database. This document is loaded from the collection given by the collection_name attribute of this class.
**kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _create_obj_from_doc_and_mongodb method is allowed here.
- Return type:
object
- classmethod from_mongodb_doc(mongodb, collection_name, doc, **kwargs)#
Create and initialize an object from a MongoDB instance and pre-loaded primary document.
- Parameters:
mongodb (pymongo.database.Database) – The MongoDB instance to load from.
collection_name (str) – The collection name within mongodb that doc was loaded from. This is needed for the sole purpose of setting the created (returned) object’s database “coordinates”.
doc (dict) – The already-retrieved main document for the object being loaded. This takes the place of giving an identifier for this object.
**kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _create_obj_from_doc_and_mongodb method is allowed here.
- Return type:
object
- classmethod remove_from_mongodb(mongodb, doc_id, collection_name=None, session=None, recursive='default')#
Remove the documents corresponding to an instance of this class from a MongoDB database.
- Parameters:
mongodb (pymongo.database.Database) – The MongoDB instance to remove documents from.
doc_id (bson.objectid.ObjectId) – The identifier of the root document stored in the database.
collection_name (str, optional) – the MongoDB collection within mongodb where the main document resides. If None, then <this_class>.collection_name is used (which is usually what you want).
session (pymongo.client_session.ClientSession, optional) – MongoDB session object to use when interacting with the MongoDB database. This can be used to implement transactions among other things.
recursive (RecursiveRemovalSpecification, optional) – An object that filters the type of documents that are removed. Used when working with inter-related experiment designs, data, and results objects to only remove the types of documents you know aren’t being shared with other documents.
- Return type:
None
- run(data, memlimit=None, comm=None, checkpoint=None, checkpoint_path=None, disable_checkpointing=False, simulator=None, optimizers=None)#
Run this protocol on data.
- Parameters:
data (ProtocolData) – The input data.
memlimit (int, optional) – A rough per-processor memory limit in bytes.
comm (mpi4py.MPI.Comm, optional) – When not
None, an MPI communicator used to run this protocol in parallel.checkpoint (StandardGSTCheckpoint, optional (default None)) – If specified use a previously generated checkpoint object to restart or warm start this run part way through.
checkpoint_path (str, optional (default None)) – A string for the path/name to use for writing intermediate checkpoint files to disk. Format is {path}/{name}, without inclusion of the json file extension. This {path}/{name} combination will have the latest completed iteration number appended to it before writing it to disk. If none, the value of {name} will be set to the name of the protocol being run.
disable_checkpointing (bool, optional (default False)) – When set to True checkpoint objects will not be constructed and written to disk during the course of this protocol. It is strongly recommended that this be kept set to False without good reason to disable the checkpoints.
simulator (ForwardSimulator.Castable or None) –
- Ignored if None. If not None, then we call
fwdsim = ForwardSimulator.cast(simulator),
and we set the .sim attribute of every Model we encounter to fwdsim.
optimizers (Optimizer, or dict, or list of Optimizer, or list of dict (default None)) – The optimizer to use, or a dictionary of optimizer parameters from which a default optimizer can be built. If a list, the length of the list should either be 1 or equal to the number of iterations. If 1, then this optimizer is used for every iteration, otherwise each optimizer is used for its corresponding iteration.
- Return type:
- run_mpi(data, num_ranks, *, mpiexec='auto', extra_mpi_args=None, ranks_per_host=None, env=None, persistent_dir=None, dry_run=False, blas_threads_per_rank=0, **run_kwargs)#
Run this protocol in parallel using MPI workers launched as a subprocess. The subprocess environment variables will be set as
where
_blas_envis inferred from blas_threads_per_rank (see Notes).This method can be called from anywhere (e.g., Jupyter notebooks or scripts) without requiring the caller to manage MPI communicators or write launcher scripts manually.
- Parameters:
data (ProtocolData) – The input data.
num_ranks (int) – Number of MPI worker processes to launch. When 1, falls back to a plain
run()call with no MPI overhead.mpiexec (str, keyword-only) – MPI launcher executable name or path.
'auto'(default) searches PATH formpiexec,mpirun, ormpiexec.hydrain that order.extra_mpi_args (list of str, keyword-only) – Extra arguments inserted between the launcher and the Python executable, e.g.
['--oversubscribe']or['--hostfile', 'hosts.txt'].ranks_per_host (int, keyword-only) – Number of ranks per virtual host. Sets
PYGSTI_MAX_HOST_PROCSin the worker environment.None(default) uses actual hostnames.env (dict, keyword-only) – Extra environment variables forwarded to worker processes, merged on top of the current
os.environ.ranks_per_hosttakes precedence overPYGSTI_MAX_HOST_PROCSsupplied here.persistent_dir (str or Path, keyword-only) – Permanent directory for data and results. Created if it does not already exist. When
None(default), the existing on-disk data path is reused if available; otherwise a temporary directory is used and deleted on return. Required whendry_run=True.dry_run (bool, keyword-only) –
When
True, we write working files topersistent_dirand returnNone. The recommended launch command is printed to console if self.verbosity is unset or positive.Requires
persistent_dirto be set.blas_threads_per_rank (int, keyword-only) – Number of threads each worker rank allows BLAS libraries to use.
0(default) auto-detects an appropriate value. See Notes.**run_kwargs – Keyword arguments forwarded to Protocol.run for each worker. Examples of arguments you might want to forward include simulator, optimizers, or disable_checkpointing. These arguments will be serialized via pickle.
- Returns:
Nonewhendry_run=True.- Return type:
ProtocolResults or None
Notes
Shared memory and host topology. pyGSTi uses a two-level communicator hierarchy: a host communicator (
host_comm) groups ranks that share a physical host and can exchange data via shared memory, while an inter-host communicator (interhost_comm) connects one rank per host for cross-host MPI. Collective operations (gather, allreduce, broadcast) use a two-phase pattern — first within the host, then across hosts — when shared memory is enabled.Setting
PYGSTI_USE_SHARED_MEMORY=0(viaenv) disables shared memory entirely. In this casehost_commisNoneand all communication goes directly through the full communicator; the host grouping has no effect.ranks_per_hostis therefore only meaningful when shared memory is enabled.Choosing
ranks_per_host. On a single compute node all ranks share one hostname, so by default pyGSTi places them all in one host group and uses shared memory across all of them. Settingranks_per_host=ksplits the ranks intonum_ranks // kvirtual hosts ofkranks each, producingk-rank shared-memory groups connected by MPI across groups. This is useful for testing multi-node behavior on a single machine, or for matching a known NUMA or hardware partition boundary.Thread oversubscription. When
blas_threads_per_rank > 0, that value is set forOMP_NUM_THREADS,OPENBLAS_NUM_THREADS,MKL_NUM_THREADS,NUMEXPR_NUM_THREADS, andBLIS_NUM_THREADSin the worker environment. Whenblas_threads_per_rank == 0(the default), the value is computed asmax(1, num_cpus // num_ranks)wherenum_cpusis the physical CPU core count of the current machine (falling back to the logical count if the physical count is unavailable). These variables are applied beforeenv, so explicit entries inenvoverride them.Job schedulers. To generate a SLURM batch script, use
stage_slurm()instead. For other schedulers (PBS, LSF, etc.), usedry_run=Truetogether withpersistent_dirto write the data and runner script to a permanent location. The recommended MPI launch command will be written to console if self.verbosity is unset or positive.
- setup_nameddict(final_dict)#
Initializes a set of nested
NamedDictdictionaries describing this protocol.This function is used by
ProtocolResultsobjects when they’re creating nested dictionaries of their contents. This function returns a set of nested, single (key,val)-pair named-dictionaries which describe the particular attributes of thisProtocolobject named within its self._nameddict_attributes tuple. The final nested dictionary is set to be final_dict, which allows additional result quantities to easily be added.
- stage_slurm(data, num_ranks, slurm, work_dir, *, ranks_per_host=None, blas_threads_per_rank=0, **run_kwargs)#
Write all working files to
work_dirand generate a SLURM batch script ready forsbatchsubmission.This is the SLURM-specific counterpart to
run_mpi(). It does not launch any processes; callsbatch <slurm.script_path>from a terminal or batch system to submit the job.- Parameters:
data (ProtocolData) – The input data.
num_ranks (int) – Total number of MPI worker processes.
slurm (SlurmSettings) – SLURM script options. See
SlurmSettingsfor details.work_dir (str or Path) – Permanent directory where data, the protocol, the pickled kwargs, and the runner script are written. All paths embedded in the generated batch script point here.
ranks_per_host (int, keyword-only) – Number of ranks per shared-memory group. When set, a commented
export PYGSTI_MAX_HOST_PROCS=<value>line is emitted in the batch script (uncomment to activate).None(default) uses a suggested value ofnum_ranks // slurm.nodes.blas_threads_per_rank (int, keyword-only) –
Number of BLAS threads per rank.
0(default) auto-detects based on the current machine’s CPU count. This value sets both--cpus-per-taskin the batch script and the BLAS environment variable exports.Note
Auto-detection measures the machine on which you call
stage_slurm(typically a login node), which may differ from the HPC compute nodes. Pass an explicit value if your compute nodes have a different core count.**run_kwargs – Forwarded to
run()in each worker. Serialized viapickle.
- Returns:
script_path – The file path of the generated SLURM script.
- Return type:
str
Notes
The generated script uses
srunas the MPI launcher.srunis SLURM-native and inherits the job allocation automatically, so no-nflag is needed.The script also contains a commented block of optional
#SBATCHdirectives (--account,--qos,--constraint, etc.) that are commonly needed but too site-specific to set automatically. Uncomment and edit the relevant lines before submitting.
- write(dirname)#
Write this protocol to a directory.
- Parameters:
dirname (str) – The directory name to write. This directory will be created if needed, and the files in an existing directory will be overwritten.
- Return type:
None
- write_to_mongodb(mongodb, session=None, overwrite_existing=False, **kwargs)#
Write this object to a MongoDB database.
The collection name used is self.collection_name, and the _id is either: 1) the ID used by a previous write or initial read-in, if one exists, OR 2) a new random bson.objectid.ObjectId
- Parameters:
mongodb (pymongo.database.Database) – The MongoDB instance to write data to.
session (pymongo.client_session.ClientSession, optional) – MongoDB session object to use when interacting with the MongoDB database. This can be used to implement transactions among other things.
overwrite_existing (bool, optional) – Whether existing documents should be overwritten. The default of False causes a ValueError to be raised if a document with the given _id already exists and is different from what is being written.
**kwargs (dict) – Additional keyword arguments potentially used by subclass implementations. Any arguments allowed by a subclass’s _add_auxiliary_write_ops_and_update_doc method is allowed here.
- Returns:
The identifier (_id value) of the main document that was written.
- Return type:
bson.objectid.ObjectId