pygsti.processors.compilationrules.CompilationRules#

class CompilationRules(compilation_rules_dict=None)#

Bases: object

A prescription for creating (“compiling”) a set of gates based on another set.

A CompilationRules object contains a dictionary of gate unitaries, much like a ProcessorSpec, and instructions for creating these gates. The instructions can be given explicitly as circuits corresponding to a given gate, or implicitly as functions. Instructions can be given for gate names (e.g. “Gx”), regardless of the target state space labels of the gate, as well as for specific gate locations (e.g. (“Gx”,2)).

Parameters:

compilation_rules_dict (dict) – A dictionary of initial rules, which can be specified in multiple formats. Keys can be either gate names as strings or gate labels as a Label object. Values are 2-tuples of (gate unitary, gate template). The gate unitary can either be a unitary matrix, function returning a matrix, or None if the gate name is a standard PyGSTi unitary. The gate template is either a Circuit with local state space labels (i.e. 0..k-1 for k qubits) or a function that takes the target gate label and returns the proper Circuit. If the key is a gate label, the gate template (second entry of the value tuple) MUST be a Circuit with absolute state space labels.

Methods

__init__([compilation_rules_dict])

add_compilation_rule(gate_name, ...[, unitary])

Add a compilation rule for a gate name, given as a circuit or function.

add_specific_compilation_rule(gate_label, ...)

Add a compilation rule for a gate at a specific location (target labels)

apply_to_circuits(circuits, **kwargs)

Use these compilation rules to convert one list of circuits into another one.

apply_to_processorspec(processor_spec[, ...])

Use these compilation rules to convert one processor specification into another one.

cast(obj)

Convert an object into compilation rules, if it isn't already.

create_aux_info()

Create auxiliary information that should be stored along with the compilation rules herein.

retrieve_compilation_of(oplabel[, force])

Get a compilation of oplabel, computing one from local templates if necessary.

add_compilation_rule(gate_name, template_circuit_or_fn, unitary=None)#

Add a compilation rule for a gate name, given as a circuit or function.

Parameters:
  • gate_name (str) – The gate name to add a rule for.

  • template_circuit_or_fn (Circuit or callable) – The rule. This can be specified as either a circuit or as a function. If a circuit is given, it must be on the gate’s local state space, assumed to be a k-qubit space (for a k-qubit gate) with qubit labels 0 to k-1. That is, the circuit must have line labels equal to 0…k-1. If a function if given, the function must take as a single argument a tuple of state space labels that specify the target labels of the gate.

  • unitary (numpy.ndarray) – The unitary corresponding to the gate. This can be left as None if gate_name names a standard or internal gate known to pyGSTi.

Return type:

None

add_specific_compilation_rule(gate_label, circuit, unitary)#

Add a compilation rule for a gate at a specific location (target labels)

Parameters:
  • gate_label (Label) – The gate label to add a rule for. Includes the gate’s name and its target state space labels (gate_label.sslbls).

  • circuit (Circuit) – The rule, given as a circuit on the gate’s local state space, i.e. the circuit’s line labels should be the same as gate_label.sslbls.

  • unitary (numpy.ndarray) – The unitary corresponding to the gate. This can be left as None if gate_label.name names a standard or internal gate known to pyGSTi.

Return type:

None

apply_to_circuits(circuits, **kwargs)#

Use these compilation rules to convert one list of circuits into another one.

Additional kwargs are passed through to Circuit.change_gate_library during translation. Common kwargs include depth_compression=False or allow_unchanged_gates=True.

Parameters:

circuits (list of Circuits) – The initial circuits, which should contain the gates present within the circuits/functions of this compilation rules object.

Return type:

list of Circuits

apply_to_processorspec(processor_spec, action='replace', gates_to_skip=None)#

Use these compilation rules to convert one processor specification into another one.

Parameters:
  • processor_spec (QubitProcessorSpec) – The initial processor specification, which should contain the gates present within the circuits/functions of this compilation rules object.

  • action ({"replace", "add"}) – Whether the existing gates in processor_spec are conveyed to the the returned processor spec. If “replace”, then they are not conveyed, if “add” they are.

  • gates_to_skip (list) – Gate names or labels to skip during processor specification construction.

Return type:

QubitProcessorSpec

classmethod cast(obj)#

Convert an object into compilation rules, if it isn’t already.

Parameters:

obj (object) – The object to convert.

Return type:

CompilationRules

create_aux_info()#

Create auxiliary information that should be stored along with the compilation rules herein.

(Currently unused, but perhaps useful in the future.)

Return type:

dict

retrieve_compilation_of(oplabel, force=False)#

Get a compilation of oplabel, computing one from local templates if necessary.

Parameters:
  • oplabel (Label) – The label of the gate to compile.

  • force (bool, optional) – If True, then an attempt is made to recompute a compilation even if oplabel already exists in this CompilationLibrary. Otherwise compilations are only computed when they are not present.

Return type:

Circuit or None, if failed to retrieve compilation