qat.plugins.PatternManager

This plugin can compile or optimize quantum circuits by replacing a subcircuit by an equivalent subcircuit. This plugin needs to be configured to perform the right changes in the circuit. A dedicated section in the user guide explains how to configure this plugin.

class qat.plugins.PatternManager(*args, **kwargs)

Defines an instance of Pattern Manager. A PatternManager will update any circuit to maximize a metric.

A local_metric will be used to calculate the local score of any pattern. The first step of this manager is to replace a pattern by an equivalent pattern having a strict better local score.

Then, if some patterns have the same best local score, a global_metric will be used to select the pattern which maximize the global score of the circuit

Parameters
  • local_metric (fun<Circuit -> float>, optional) – local metric

  • global_metric (fun<Circuit -> float>, optional) – global metric

  • groups (list<PatternGroup>, optional) – list of groups to link to this pattern manager

  • collections (list<collections>, optional) – list of collections to link to this pattern manager. A collection is a list of PatternGroup

  • gate_set (GateSet, optional) – Gate set used to cast GraphCircuit objects used internally into Circuit objects

add_abstract_gate()

Internally, the PatternManager does not manipulate Circuit objects. During the optimization, the PatternManager must be able to cast data structures used internally into Circuit (to compute metrics or to return a Circuit at the end of the optimization).

This method is used to define gates that are not part of the default gate set (to use these gates in some patterns):

from qat.lang.AQASM import AbstractGate
from qat.pbo import PatternManager

# Define an abstract gate
my_gate = AbstractGate("MY_GATE", [], 1)

# Define a manager
manager = PatternManager()
manager.add_abstract_gate(my_gate)
print("The gate 'MY_GATE' can now be used in patterns of 'manager'")
The gate 'MY_GATE' can now be used in patterns of 'manager'

Note

The constructor of PatternManager accepts a GateSet in argument. Passing a gate set in argument or adding abstract gates one by one are equivalent

Parameters

abstract_gate (AbstractGate) – abstract gate

compile(batch, specs)

Compile a Batch. This method will call the method replace_pattern() to optimize each job composing the batch (to override the default arguments of this method, please refer to set_compile_attributes())

Parameters
  • batch (Batch) – batch to optimize

  • specs (HardwareSpecs) – specs of the QPU that this plugin will optimize for

Returns

optimized batch

Return type

Batch

new_group()

Return a PatternGroup linked to this PatternManager. A group is a set of equivalent patterns. Created groups are used to perform local changes in circuits to optimize them. Please refer the method replace_pattern() for further information.

from qat.pbo import PatternManager

manager = PatternManager()

# Define a a group of equivalent patterns
group = manager.new_group()
group.add_pattern([("SWAP", {0, 1})])
group.add_pattern([("CNOT", [0, 1]), ("CNOT", [1, 0]), ("CNOT", [0, 1])])
Returns

PatternGroup

replace_pattern()

Replace some patterns in a circuit by an equivalent one with to improve the circuit

Parameters
  • circ (qat.core.Circuit or qat.pbo.GraphCircuit) –

  • method (str, optional) –

    heuristic used to optimize globally. Two heuristics are implemented:

    • ”gradient” (default) -> Gradient Descent

    • ”annealing” -> Simulated annealing

  • window_size (int, optional) – size of the window for the global optimization - used if method == "gradient" (Default value: 1)

  • max_iterations (int, optional) – maximum number of iteration for the global optimization - used if method == "annealing" (Default value: 250)

  • temperature (int, optional) – temperature of the simulated annealing - used if method == "annealing" (Default value: 1000)

  • trace (list, optional) – if a list is given, values of the metric will be inserted at each step

Returns

Resulting circuit. The result is either a qat.core.Circuit or None

  • If a circuit was given in argument: the result is an optimized equivalent circuit

  • If a graph was given in argument: the optimal circuit is contained in the GraphCircuit object given in argument

Return type

Circuit or None

set_compile_attributes(**kwargs)

The compile() method will call the method replace_pattern() to optimize each job composing the batch. This method will override the default arguments used by the method replace_pattern().

from qat.plugins import PatternManager

manager = PatternManager()
manager.set_compile_attributes(method="annealing")
print("The 'compile' method will use the 'annealing' method to optimize "
      "the batches")
The 'compile' method will use the 'annealing' method to optimize the batches

Note

Parameters used for the optimization could be overrided for a specific batch, by updating the meta-data of this batch

from json import dumps as json_dumps
from qat.core import Batch

# Define a batch
batch = Batch(jobs=[...])

# Define the options to optimize this batch
options = dict(method="gradient", window_size=2)
batch.meta_data = {"PatternManager": json_dumps(options)}
Keyword Arguments
  • method (str, optional) – optimization method (cf. replace_pattern() for more information)

  • window_size (int, optional) – size of windows used during optimization (cf. replace_pattern() for more information)

  • max_iterations (int, optional) – maximal number of iteration (cf. replace_pattern() for more information)

  • temparature (int, optional) – temperature used for the simulated annealing (cf. replace_pattern() for more information)