pygsti.forwardsims.termforwardsim.TermForwardSimulator#

class TermForwardSimulator(model=None, mode='pruned', max_order=3, desired_perr=0.01, allowed_perr=0.1, min_term_mag=None, max_paths_per_outcome=1000, perr_heuristic='none', max_term_stages=5, path_fraction_threshold=0.9, oob_check_interval=10, cache=None, num_atoms=None, processor_grid=None, param_blk_sizes=None)#

Bases: DistributableForwardSimulator

A forward-simulation calculator that uses term-path-integration to compute probabilities.

Parameters:
  • model (Model, optional) – The parent model of this simulator. It’s fine if this is None at first, but it will need to be set (by assigning self.model before using this simulator.

  • mode ({"taylor-order", "pruned", "direct"}) – Overall method used to compute (approximate) circuit probabilities. The “taylor-order” mode includes all taylor-expansion terms up to a fixed and pre-defined order, fixing a single “path set” at the outset. The “pruned” mode selects a path set based on a heuristic (sometimes a true upper bound) calculation of the error in the approximate probabilities. This method effectively “prunes” the paths to meet a fixed desired accuracy. The “direct” method is still under development. Its intention is to perform path integrals directly without the use of polynomial computation and caching. Initial testing showed the direct method to be much slower for common QCVV tasks, making it less urgent to complete.

  • max_order (int) – The maximum order of error-rate terms to include in probability computations. When polynomials are computed, the maximum Taylor order to compute polynomials to.

  • desired_perr (float, optional) – The desired maximum-error when computing probabilities.. Path sets are selected (heuristically) to target this error, within the bounds set by max_order, etc.

  • allowed_perr (float, optional) – The allowed maximum-error when computing probabilities. When rigorous bounds cannot guarantee that probabilities are correct to within this error, additional paths are added to the path set.

  • min_term_mag (float, optional) – Terms with magnitudes less than this value will be ignored, i.e. not considered candidates for inclusion in paths. If this number is too low, the number of possible paths to consder may be very large, impacting performance. If too high, then not enough paths will be considered to achieve an accurate result. By default this value is set automatically based on the desired error and max_paths_per_outcome. Only adjust this if you know what you’re doing.

  • max_paths_per_outcome (int, optional) – The maximum number of paths that can be used (summed) to compute a single outcome probability.

  • perr_heuristic ({"none", "scaled", "meanscaled"}) – Which heuristic (if any) to use when deciding whether a given path set is sufficient given the allowed error (allowed_perr). - “none”: This is the strictest setting, and is absence of any heuristic. if the path-magnitude gap (the maximum - achieved sum-of-path-magnitudes, a rigorous upper bound on the approximation error for a circuit outcome probability) is greater than allowed_perr for any circuit, the path set is deemed insufficient. - “scaled”: a path set is deemed insufficient when, for any circuit, the path-magnitude gap multiplied by a scaling factor is greater than allowed_perr. This scaling factor is equal to the computed probability divided by the achieved sum-of-path-magnitudes and is always less than 1. This scaling is essentially the ratio of the sum of the path amplitudes without and with an absolute value, and tries to quantify and offset the degree of pessimism in the computed path-magnitude gap. - “meanscaled” : a path set is deemed insufficient when, the mean of all the scaled (as above) path-magnitude gaps is greater than allowed_perr. The mean here is thus over the circuit outcomes. This heuristic is even more permissive of potentially bad path sets than “scaled”, as it allows badly approximated circuits to be offset by well approximated ones.

  • max_term_stages (int, optional) – The maximum number of “stage”, i.e. re-computations of a path set, are allowed before giving up.

  • path_fraction_threshold (float, optional) – When greater than this fraction of the total available paths (set by other constraints) are considered, no further re-computation of the path set will occur, as it is expected to give little improvement.

  • oob_check_interval (int, optional) – The optimizer will check whether the computed probabilities have sufficiently small error every oob_check_interval (outer) optimizer iteration.

  • cache (dict, optional) – A dictionary of pre-computed compact polynomial objects. Keys are (max_order, rholabel, elabel, circuit) tuples, where max_order is an integer, rholabel and elabel are Label objects, and circuit is a Circuit. Computed values are added to any dictionary that is supplied, so supplying an empty dictionary and using this calculator will cause the dictionary to be filled with values.

  • num_atoms (int, optional) – The number of atoms (sub-tables) to use when creating the layout (i.e. when calling create_layout()). This determines how many units the element (circuit outcome probability) dimension is divided into, and doesn’t have to correclate with the number of processors. When multiple processors are used, if num_atoms is less than the number of processors then num_atoms should divide the number of processors evenly, so that num_atoms // num_procs groups of processors can be used to divide the computation over parameter dimensions.

  • processor_grid (tuple optional) – Specifies how the total number of processors should be divided into a number of atom-processors, 1st-parameter-deriv-processors, and 2nd-parameter-deriv-processors. Each level of specification is optional, so this can be a 1-, 2-, or 3- tuple of integers (or None). Multiplying the elements of processor_grid together should give at most the total number of processors.

  • param_blk_sizes (tuple, optional) – The parameter block sizes along the first or first & second parameter dimensions - so this can be a 0-, 1- or 2-tuple of integers or None values. A block size of None means that there should be no division into blocks, and that each block processor computes all of its parameter indices at once.

Methods

__init__([model, mode, max_order, ...])

add_mongodb_write_ops(write_ops, mongodb[, ...])

Accumulate write and update operations for writing this object to a MongoDB database.

bulk_dprobs(circuits[, resource_alloc, smartc])

Construct a dictionary containing the probability derivatives for an entire list of circuits.

bulk_fill_dprobs(array_to_fill, layout[, ...])

Compute the outcome probability-derivatives for an entire tree of circuits.

bulk_fill_hprobs(array_to_fill, layout[, ...])

Compute the outcome probability-Hessians for an entire list of circuits.

bulk_fill_probs(array_to_fill, layout)

Compute the outcome probabilities for a list circuits.

bulk_hprobs(circuits[, resource_alloc, smartc])

Construct a dictionary containing the probability Hessians for an entire list of circuits.

bulk_probs(circuits[, clip_to, ...])

Construct a dictionary containing the probabilities for an entire list of circuits.

bulk_sopm_gaps(layout)

Compute an element array sum-of-path-magnitude gaps (the difference between maximum and achieved).

bulk_sopm_gaps_jacobian(layout)

Compute the jacobian of the the output of bulk_sopm_gaps().

bulk_test_if_paths_are_sufficient(layout, probs)

Determine whether layout's current path set (perhaps heuristically) achieves the desired accuracy.

cast(obj[, num_qubits])

num_qubits only used if obj == 'auto'

copy([keep_model_attached])

Return a shallow copy of this TermForwardSimulator.

create_copa_layout_circuit_cache(circuits, model)

Helper function for pre-computing/pre-processing circuits structures used in matrix layout creation.

create_layout(circuits[, dataset, ...])

Constructs an circuit-outcome-probability-array (COPA) layout for a list of circuits.

dprobs(circuit[, resource_alloc])

Construct a dictionary containing outcome probability derivatives for a single circuit.

dump(f[, format])

Serializes and writes this object to a given output stream.

dumps([format])

Serializes this object and returns it as a string.

find_minimal_paths_set(layout[, ...])

Find a good, i.e. minimal, path set for the current model-parameter space point.

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.

from_nice_serialization(state)

Create and initialize an object from a "nice" serialization.

hprobs(circuit[, resource_alloc])

Construct a dictionary containing outcome probability Hessians for a single circuit.

iter_hprobs_by_rectangle(layout, wrt_slices_list)

Iterates over the 2nd derivatives of a layout's circuit probabilities one rectangle at a time.

load(f[, format])

Load an object of this type, or a subclass of this type, from an input stream.

loads(s[, format])

Load an object of this type, or a subclass of this type, from a string.

probs(circuit[, outcomes, time, resource_alloc])

Construct a dictionary containing the outcome probabilities for a single circuit.

product(circuit[, scale])

Compute the product of a specified sequence of operation labels.

read(path[, format])

Read an object of this type, or a subclass of this type, from a file.

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, ...])

select_paths_set(layout, path_set)

Selects (makes "current") a path set and computes polynomials the new set.

to_nice_serialization()

Serialize this object in a way that adheres to "niceness" rules of common text file formats.

write(path, **format_kwargs)

Writes this object to a file.

write_to_mongodb(mongodb[, session, ...])

Write this object to a MongoDB database.

Attributes

Castable

alias of ForwardSimulator | Callable[[], ForwardSimulator] | Literal['map'] | Literal['matrix'] | Literal['auto']

collection_name

model

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 to write_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

bulk_dprobs(circuits, resource_alloc=None, smartc=None)#

Construct a dictionary containing the probability derivatives for an entire list of circuits.

Parameters:
  • circuits (list of Circuits) – The list of circuits. May also be a CircuitOutcomeProbabilityArrayLayout object containing pre-computed quantities that make this function run faster.

  • resource_alloc (ResourceAllocation, optional) – A resource allocation object describing the available resources and a strategy for partitioning them.

  • smartc (SmartCache, optional) – A cache object to cache & use previously cached values inside this function.

Returns:

dprobs – A dictionary such that dprobs[circuit] is an ordered dictionary of derivative arrays (one element per differentiated parameter) whose keys are outcome labels

Return type:

dictionary

bulk_fill_dprobs(array_to_fill, layout, pr_array_to_fill=None)#

Compute the outcome probability-derivatives for an entire tree of circuits.

This routine fills a 2D array, array_to_fill with circuit outcome probabilities as dictated by a CircuitOutcomeProbabilityArrayLayout (“COPA layout”) object, which is usually specifically tailored for efficiency.

The array_to_fill array must have length equal to the number of elements in layout, and the meanings of each element are given by layout.

Parameters:
  • array_to_fill (numpy ndarray) – an already-allocated 2D numpy array of shape (len(layout), Np), where Np is the number of model parameters being differentiated with respect to.

  • layout (CircuitOutcomeProbabilityArrayLayout) – A layout for array_to_fill, describing what circuit outcome each element corresponds to. Usually given by a prior call to create_layout().

  • pr_mx_to_fill (numpy array, optional) – when not None, an already-allocated length-len(layout) numpy array that is filled with probabilities, just as in bulk_fill_probs().

Return type:

None

bulk_fill_hprobs(array_to_fill, layout, pr_array_to_fill=None, deriv1_array_to_fill=None, deriv2_array_to_fill=None)#

Compute the outcome probability-Hessians for an entire list of circuits.

Similar to bulk_fill_probs(…), but fills a 3D array with the Hessians for each circuit outcome probability.

Parameters:
  • array_to_fill (numpy ndarray) – an already-allocated numpy array of shape (len(layout),M1,M2) where M1 and M2 are the number of selected model parameters (by wrt_filter1 and wrt_filter2).

  • layout (CircuitOutcomeProbabilityArrayLayout) – A layout for array_to_fill, describing what circuit outcome each element corresponds to. Usually given by a prior call to create_layout().

  • pr_mx_to_fill (numpy array, optional) – when not None, an already-allocated length-len(layout) numpy array that is filled with probabilities, just as in bulk_fill_probs().

  • deriv1_array_to_fill (numpy array, optional) – when not None, an already-allocated numpy array of shape (len(layout),M1) that is filled with probability derivatives, similar to bulk_fill_dprobs() (see array_to_fill for a definition of M1).

  • deriv2_array_to_fill (numpy array, optional) – when not None, an already-allocated numpy array of shape (len(layout),M2) that is filled with probability derivatives, similar to bulk_fill_dprobs() (see array_to_fill for a definition of M2).

Return type:

None

bulk_fill_probs(array_to_fill, layout)#

Compute the outcome probabilities for a list circuits.

This routine fills a 1D array, array_to_fill with circuit outcome probabilities as dictated by a CircuitOutcomeProbabilityArrayLayout (“COPA layout”) object, which is usually specifically tailored for efficiency.

The array_to_fill array must have length equal to the number of elements in layout, and the meanings of each element are given by layout.

Parameters:
  • array_to_fill (numpy ndarray) – an already-allocated 1D numpy array of length equal to the total number of computed elements (i.e. len(layout)).

  • layout (CircuitOutcomeProbabilityArrayLayout) – A layout for array_to_fill, describing what circuit outcome each element corresponds to. Usually given by a prior call to create_layout().

Return type:

None

bulk_hprobs(circuits, resource_alloc=None, smartc=None)#

Construct a dictionary containing the probability Hessians for an entire list of circuits.

Parameters:
  • circuits (list of Circuits) – The list of circuits. May also be a CircuitOutcomeProbabilityArrayLayout object containing pre-computed quantities that make this function run faster.

  • resource_alloc (ResourceAllocation, optional) – A resource allocation object describing the available resources and a strategy for partitioning them.

  • smartc (SmartCache, optional) – A cache object to cache & use previously cached values inside this function.

Returns:

hprobs – A dictionary such that hprobs[circuit] is an ordered dictionary of Hessian arrays (a square matrix with one row/column per differentiated parameter) whose keys are outcome labels

Return type:

dictionary

bulk_probs(circuits, clip_to=None, resource_alloc=None, smartc=None)#

Construct a dictionary containing the probabilities for an entire list of circuits.

Parameters:
  • circuits (list of Circuits) – The list of circuits. May also be a CircuitOutcomeProbabilityArrayLayout object containing pre-computed quantities that make this function run faster.

  • clip_to (2-tuple, optional) – (min,max) to clip return value if not None.

  • resource_alloc (ResourceAllocation, optional) – A resource allocation object describing the available resources and a strategy for partitioning them.

  • smartc (SmartCache, optional) – A cache object to cache & use previously cached values inside this function.

Returns:

probs – A dictionary such that probs[circuit] is an ordered dictionary of outcome probabilities whose keys are outcome labels.

Return type:

dictionary

bulk_sopm_gaps(layout)#

Compute an element array sum-of-path-magnitude gaps (the difference between maximum and achieved).

These values are computed for the current set of paths contained in eval_tree.

Parameters:

layout (TermCOPALayout) – The layout specifying the quantities (circuit outcome probabilities) to be computed, and related information.

Returns:

An array containing the per-circuit-outcome sum-of-path-magnitude gaps.

Return type:

numpy.ndarray

bulk_sopm_gaps_jacobian(layout)#

Compute the jacobian of the the output of bulk_sopm_gaps().

Parameters:
  • layout (TermCOPALayout) – The layout specifying the quantities (circuit outcome probabilities) to be computed, and related information.

  • resource_alloc (ResourceAllocation, optional) – Available resources for this computation. Includes the number of processors (MPI comm) and memory limit.

Returns:

An number-of-elements by number-of-model-parameters array containing the jacobian of the sum-of-path-magnitude gaps.

Return type:

numpy.ndarray

bulk_test_if_paths_are_sufficient(layout, probs, verbosity=0)#

Determine whether layout’s current path set (perhaps heuristically) achieves the desired accuracy.

The current path set is determined by the current (per-circuti) path-magnitude thresholds (stored in the evaluation tree) and the current parameter-space point (also reflected in the terms cached in the evaluation tree).

Parameters:
  • layout (TermCOPALayout) – The layout specifying the quantities (circuit outcome probabilities) to be computed, and related information.

  • probs (numpy.ndarray) – The element array of (approximate) circuit outcome probabilities. This is needed because some heuristics take into account an probability’s value when computing an acceptable path-magnitude gap.

  • verbosity (int or VerbosityPrinter, optional) – An integer verbosity level or printer object for displaying messages.

Return type:

bool

classmethod cast(obj, num_qubits=None)#

num_qubits only used if obj == ‘auto’

Parameters:

obj (ForwardSimulator | Callable[[], ForwardSimulator] | Literal['map'] | ~typing.Literal['matrix'] | ~typing.Literal['auto'])

copy(keep_model_attached=True)#

Return a shallow copy of this TermForwardSimulator.

Return type:

TermForwardSimulator

static create_copa_layout_circuit_cache(circuits, model, dataset=None)#

Helper function for pre-computing/pre-processing circuits structures used in matrix layout creation.

create_layout(circuits, dataset=None, resource_alloc=None, array_types=('E',), derivative_dimension=None, verbosity=0, layout_creation_circuit_cache=None)#

Constructs an circuit-outcome-probability-array (COPA) layout for a list of circuits.

Parameters:
  • circuits (list) – The circuits whose outcome probabilities should be included in the layout.

  • dataset (DataSet) – The source of data counts that will be compared to the circuit outcome probabilities. The computed outcome probabilities are limited to those with counts present in dataset.

  • resource_alloc (ResourceAllocation) – A available resources and allocation information. These factors influence how the layout (evaluation strategy) is constructed.

  • array_types (tuple, optional) – A tuple of string-valued array types. See ForwardSimulator.create_layout().

  • derivative_dimension (int, optional) – Optionally, the parameter-space dimension used when taking first and second derivatives with respect to the circuit outcome probabilities. This must be non-None when array_types contains ‘ep’ or ‘epp’ types.

  • verbosity (int or VerbosityPrinter) – Determines how much output to send to stdout. 0 means no output, higher integers mean more output.

  • layout_creation_circuit_cache – A precomputed dictionary serving as a cache for completed circuits. I.e. circuits with prep labels and POVM labels appended. Along with other useful pre-computed circuit structures used in layout creation.

Return type:

TermCOPALayout

dprobs(circuit, resource_alloc=None)#

Construct a dictionary containing outcome probability derivatives for a single circuit.

Parameters:
  • circuit (Circuit or tuple of operation labels) – The sequence of operation labels specifying the circuit.

  • resource_alloc (ResourceAllocation, optional) – The resources available for computing circuit outcome probability derivatives.

Returns:

dprobs – A dictionary with keys equal to outcome labels and values equal to an array containing the (partial) derivatives of the outcome probability with respect to all model parameters.

Return type:

OutcomeLabelDict

dump(f, format='json', **format_kwargs)#

Serializes and writes this object to a given output stream.

Parameters:
  • f (file-like) – A writable output stream.

  • format ({'json', 'repr'}) – The format to write.

  • format_kwargs (dict, optional) – Additional arguments specific to the format being used. For example, the JSON format accepts indent as an argument because json.dump does.

Return type:

None

dumps(format='json', **format_kwargs)#

Serializes this object and returns it as a string.

Parameters:
  • format ({'json', 'repr'}) – The format to write.

  • format_kwargs (dict, optional) – Additional arguments specific to the format being used. For example, the JSON format accepts indent as an argument because json.dump does.

Return type:

str

find_minimal_paths_set(layout, exit_after_this_many_failures=0)#

Find a good, i.e. minimal, path set for the current model-parameter space point.

Parameters:
  • layout (TermCOPALayout) – The layout specifying the quantities (circuit outcome probabilities) to be computed, and related information.

  • exit_after_this_many_failures (int, optional) – If > 0, give up after this many circuits fail to meet the desired accuracy criteria. This short-circuits doomed attempts to find a good path set so they don’t take too long.

Return type:

TermPathSet

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 from_nice_serialization(state)#

Create and initialize an object from a “nice” serialization.

A “nice” serialization here means one created by a prior call to to_nice_serialization using this class or a subclass of it. Nice serializations adhere to additional rules (e.g. that dictionary keys must be strings) that make them amenable to common file formats (e.g. JSON).

The state argument is typically a dictionary containing ‘module’ and ‘state’ keys specifying the type of object that should be created. This type must be this class or a subclass of it.

Parameters:

state (object) – An object, usually a dictionary, representing the object to de-serialize.

Return type:

object

hprobs(circuit, resource_alloc=None)#

Construct a dictionary containing outcome probability Hessians for a single circuit.

Parameters:
  • circuit (Circuit or tuple of operation labels) – The sequence of operation labels specifying the circuit.

  • resource_alloc (ResourceAllocation, optional) – The resources available for computing circuit outcome probability Hessians.

Returns:

hprobs – A dictionary with keys equal to outcome labels and values equal to a 2D array that is the Hessian matrix for the corresponding outcome probability (with respect to all model parameters).

Return type:

OutcomeLabelDict

iter_hprobs_by_rectangle(layout, wrt_slices_list, return_dprobs_12=False)#

Iterates over the 2nd derivatives of a layout’s circuit probabilities one rectangle at a time.

This routine can be useful when memory constraints make constructing the entire Hessian at once impractical, and as it only computes a subset of the Hessian’s rows and columns (a “rectangle”) at once. For example, the Hessian of a function of many circuit probabilities can often be computed rectangle-by-rectangle and without the need to ever store the entire Hessian at once.

Parameters:
  • layout (CircuitOutcomeProbabilityArrayLayout) – A layout for generated arrays, describing what circuit outcome each element corresponds to. Usually given by a prior call to create_layout().

  • wrt_slices_list (list) – A list of (rowSlice,colSlice) 2-tuples, each of which specify a “rectangle” of the Hessian to compute. Iterating over the output of this function iterates over these computed rectangles, in the order given by wrt_slices_list. rowSlice and colSlice must by Python slice objects.

  • return_dprobs_12 (boolean, optional) – If true, the generator computes a 2-tuple: (hessian_col, d12_col), where d12_col is a column of the matrix d12 defined by: d12[iSpamLabel,iOpStr,p1,p2] = dP/d(p1)*dP/d(p2) where P is is the probability generated by the sequence and spam label indexed by iOpStr and iSpamLabel. d12 has the same dimensions as the Hessian, and turns out to be useful when computing the Hessian of functions of the probabilities.

Returns:

A generator which, when iterated, yields the 3-tuple (rowSlice, colSlice, hprobs) or (rowSlice, colSlice, hprobs, dprobs12) (the latter if return_dprobs_12 == True). rowSlice and colSlice are slices directly from wrt_slices_list. hprobs and dprobs12 are arrays of shape E x B x B’, where:

  • E is the length of layout elements

  • B is the number of parameter rows (the length of rowSlice)

  • B’ is the number of parameter columns (the length of colSlice)

If mx, dp1, and dp2 are the outputs of bulk_fill_hprobs() (i.e. args mx_to_fill, deriv1_mx_to_fill, and deriv2_mx_to_fill), then:

  • hprobs == mx[:,rowSlice,colSlice]

  • dprobs12 == dp1[:,rowSlice,None] * dp2[:,None,colSlice]

Return type:

rectangle_generator

classmethod load(f, format='json')#

Load an object of this type, or a subclass of this type, from an input stream.

Parameters:
  • f (file-like) – An open input stream to read from.

  • format ({'json'}) – The format of the input stream data.

Return type:

NicelySerializable

classmethod loads(s, format='json')#

Load an object of this type, or a subclass of this type, from a string.

Parameters:
  • s (str) – The serialized object.

  • format ({'json'}) – The format of the string data.

Return type:

NicelySerializable

probs(circuit, outcomes=None, time=None, resource_alloc=None)#

Construct a dictionary containing the outcome probabilities for a single circuit.

Parameters:
  • circuit (Circuit or tuple of operation labels) – The sequence of operation labels specifying the circuit.

  • outcomes (list or tuple) – A sequence of outcomes, which can themselves be either tuples (to include intermediate measurements) or simple strings, e.g. ‘010’. If None, only non-zero outcome probabilities will be reported.

  • time (float, optional) – The start time at which circuit is evaluated.

  • resource_alloc (ResourceAllocation, optional) – The resources available for computing circuit outcome probabilities.

Returns:

probs – A dictionary with keys equal to outcome labels and values equal to probabilities. If no target outcomes provided, only non-zero probabilities will be reported.

Return type:

OutcomeLabelDict

product(circuit, scale=False)#

Compute the product of a specified sequence of operation labels.

Note: LinearOperator matrices are multiplied in the reversed order of the tuple. That is, the first element of circuit can be thought of as the first gate operation performed, which is on the far right of the product of matrices.

Parameters:
  • circuit (Circuit or tuple of operation labels) – The sequence of operation labels.

  • scale (bool, optional) – When True, return a scaling factor (see below).

Returns:

  • product (numpy array) – The product or scaled product of the operation matrices.

  • scale (float) – Only returned when scale == True, in which case the actual product == product * scale. The purpose of this is to allow a trace or other linear operation to be done prior to the scaling.

classmethod read(path, format=None)#

Read an object of this type, or a subclass of this type, from a file.

Parameters:
  • path (str or Path or file-like) – The filename to open or an already open input stream.

  • format ({'json', None}) – The format of the file. If None this is determined automatically by the filename extension of a given path.

Return type:

NicelySerializable

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

select_paths_set(layout, path_set)#

Selects (makes “current”) a path set and computes polynomials the new set.

Parameters:
  • layout (TermCOPALayout) – The layout whose path-set should be set.

  • path_set (PathSet) – The path set to select.

Return type:

None

to_nice_serialization()#

Serialize this object in a way that adheres to “niceness” rules of common text file formats.

Returns:

Usually a dictionary representing the serialized object, but may also be another native Python type, e.g. a string or list.

Return type:

object

write(path, **format_kwargs)#

Writes this object to a file.

Parameters:
  • path (str or Path) – The name of the file that is written.

  • format_kwargs (dict, optional) – Additional arguments specific to the format being used. For example, the JSON format accepts indent as an argument because json.dump does.

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