5.4. Exploration and extraction
In the previous section, we proposed a data structure that is confluently persistent and can be used to explore the space of all possible transformations of a graph transformation system (GTS). We are now interested in using to solve optimisation problems over the space of reachable graphs in the GTS. Following the blueprint of equality saturation (see section 5.2), we proceed in two phases:
- Exploration. Given an input graph , populate with events that correspond to rewrites applicable to graphs reachable from ,
- Extraction. Given a cost function , extract the optimal graph in , i.e. the graph that is a flattening of a set of compatible edits and minimises .
Each phase comes with its respective challenges, which we discuss in this section. We will first look at the exploration phase, which requires a way to find and construct new events that can be added to . We will consider the extraction phase in the second part of this section and see that the problem of optimisation over the power set can be reduced to boolean satisfiability formula that admit simple cost functions in the use cases of interest.
There is an additional open question that we do not cover in this section and would merit a study of its own: the choice of heuristics that guide the exploration phase to ensure the “most interesting parts” of the GTS rewrite space are explored. We propose a very simple heuristic to this end in the benchmarks of section 5.5, but further investigations are called for.
Exploring the data structure with pattern matching #
We established in the previous section that rewrites that apply on can
equivalently be added as events to . In other words, a graph
is reachable from using the rewrites of a GTS if and only if there is a set
of compatible events such that is the graph
obtained from FlattenHistory on input .
To expand to a larger set , we
must find all applicable rewrites on all graphs within . A naive
solution would iterate over all subsets of , check
whether they form a compatible set of events, compute FlattenHistory if they
do, and finally run pattern matching on the obtained graph to find the
applicable rewrites. We can do better.
The idea is to traverse the set of events in using the glueing relations that connect vertices between events. Define the function that is the union of all glueing relations in events in :
where we write for the owner of , i.e. the (unique) event
such that . We define the set
of equivalent vertices of that are compatible with by
applying recursively and filtering out vertices whose owner is not
compatible with . It is easiest to formalise this definition using pseudocode
for the EquivalentVertices procedure. The set of vertices in
are vertices of descendant events of .
def EquivalentVertices(
v: Vertex, events: Set[Event]
) -> Set[Vertex]:
all_vertices = set({v})
for w in mu_bar(v):
new_events = union(events, {owner(w)})
if AreCompatible(new_events):
all_vertices = union(all_vertices,
EquivalentVertices(w, new_events)
)
return all_vertices
Whilst it looks as though EquivalentVertices does not depend on ,
it does so through the use of the function calls to mu_bar.
We use EquivalentVertices to repeatedly extend a set of pinned vertices
. A set of pinned vertices must satisfy two
properties:
- the set is a set of compatible events,
- there is no vertex and event such that .
As a result, for the flattened graph , it always holds that
. Furthermore, if is the subgraph of
induced by , then for any superset of pinned vertices
, we have where
. In other words: extending a set of pinned vertices
results in an extension of the flattened graph – a very useful property when
pattern matching. This property follows from the second property above and the
definition of FlattenHistory.
This gives us the following simple procedure for pattern matching:
- Start with a single pinned vertex .
- Construct partial embeddings for patterns .
- Pick a new vertex in but not in (that we would like to extend the domain of definition of our pattern embeddings to).
- For all vertices , build new pinned vertex sets , filter out the sets that are not valid pinned vertex sets.
- Repeat steps 2–4 until all pattern embeddings have been found.
Step 1 is straightforward – notice that pattern matching must be started at a vertex in , so finding all patterns will require iterating over all choices of . The pattern embeddings are constructed over iterations of step 2: each iteration can be seen as one step of the pattern matcher – for instance, as presented in chapter 4 – extending the pattern embeddings that can be extended and discarding those that cannot. If all possible pattern embeddings have been discarded, then matching can be aborted for that set.
How step 3 should be implemented depends on the types of graphs and patterns that are matched on. It is straightforward in the case of computation graphs with only linear values, i.e. hypergraphs with hyperedges that have directed, ordered endpoints and vertices that are incident to exactly one incoming and one outgoing edge. In that case, can always be chosen in such a way as to ensure progress on the next iteration of step 2, i.e. the domain of definition of at least one partial pattern embedding will be extended by one vertex. The text in the blue box below explains this case in more detail.
Step 4 produces all possible extensions of to pinned vertex sets that include a descendant of (or itself). All vertices in are in events compatible with by definition, so to check that is a valid pinned vertex set, we only need to check the second property of pinned vertices. Let be a pattern, let be the set of all sets under consideration. Step 4 increments the sizes of all pinned vertex sets whilst maintaining the following invariant.
Invariant for step 4. If there is a superset of compatible events such that embeds in , then there is a superset of vertices such that embeds in .
Finally, step 5 ensures the process is repeated until, for all partial pattern embeddings, either the domain of definition is complete, or the embedding of is not possible. Given that step 4 increments the size of sets at each iteration, this will terminate as long as the vertex picking strategy of step 3 selects vertices that allow to extend (or refute) the partial pattern embeddings constructed and extended in step 2. This is satisfied, for example, in the case of linear minIR graphs, as explained in the box.
Choosing the next vertex to pin in linear minIR (step 3). Assuming patterns are connected, for any partial pattern embedding there is an edge with no image in but such that at least one of the endvertex of has an image in – say, is the outgoing edge of . Let be an endvertex of in that has no image in – and say, it is the -th outgoing endvertex of in .
Then uniquely identifies an edge in – the unique outgoing edge of – which, in turn, uniquely identifies a vertex – the -th outgoing endvertex of . By choosing in step 3, step 4 will create pinned vertex sets that include all possible vertices equivalent to , which are all vertices that might be connected to through its outgoing edge1. The next iteration of step 2 will then either extend the partial pattern embedding to or conclude that an embedding of is not possible.
Using the approach just sketched, pattern matching can be performed on the persistent data structure . The runtime of steps 2 and 3 depend on the type of graphs and patterns that are matched on – these are, however, typical problems that appear in most instances of pattern matching, independently of the data structure used here. A concrete approach to pattern matching and results for the graph types of interest to quantum compilation was presented in chapter 4.
The runtime of step 4 and the number of overall iterations of steps 2–4
required for pattern matching will depend on the number of events in
(AreCompatible runs in runtime linear in the number of
ancestors), the number of equivalent vertices that successive rounds of step 4
will return and the types of patterns and pattern matching strategies.
Extraction using SAT #
Moving on to the extraction phase, we are now interested in extracting the optimal graph from , according to some cost function of interest. Unlike exploring the “naive” search space of all graphs reachable in the GTS, the optimal solution within the persistent data structure cannot simply be read out.
We showed in section 5.3 that finding an optimal graph that is the result of a sequence of rewrites on an input graph is equivalent to finding an optimal set of compatible events – the optimal graph is then recoved by taking .
There are elements in , which we encode as a boolean assignment problem by introducing a boolean variable for all events . The set of events is then given by
We can constrain the boolean assignments to compatible sets by introducing a boolean formula
for all such that their vertex deletion sets intersect . Any assignment of that satisfies all constraints of this format defines a compatible set of events.
How many such pairs of events ) are there? By definition of parents, two events and can only have overlapping vertex deletion sets if they share a parent. Assuming all events have at most children, ensuring is a set of compatible events requires at most constraints.
To further restrict to , i.e. to sets of compatible events that contain all ancestors, we can add the further constraints: implies . This introduces up to implication constraints
for all such that .
For any set of events , the conjunction of all constraints presented above, i.e. the event compatibility constraints (1) and the parent-child relation constraints (2), defines a boolean satisfiability problem (SAT) with variables . We have shown:
Consider a GTS with a constant upper bound on the number of rewrites that may overlap any previous rewrite.
The set of valid sequences of rewrites that can be extracted from a set of events in the GTS is given by the set of satisfying assignments of a SAT problem Cook, 1971. 1971. The complexity of theorem-proving procedures. In Proceedings of the third annual ACM symposium on Theory of computing - STOC ’71. ACM Press, 151--158. doi: 10.1145/800157.805047 Moskew., 2001. 2001. Chaff: engineering an efficient SAT solver. In Proceedings of the 38th conference on Design automation - DAC ’01. ACM Press, 530--535. doi: 10.1145/378239.379017 with variables of size .
Finding the optimal assignment #
We now have to find the optimal assignment among all satisfiable assignments for the SAT problem given above. In the most general case where the cost function to be minimised is given as a black box oracle on the graph , i.e. on the flattened history of the solution set , this optimisation problem is hard2.
However, if can be expressed as a function of instead of the flattened history , then the ‘hardness’ can be encapsulated within an instance of a SMT problem (satisfiability modulo theories Nieuwe., 2006. 2006. On SAT Modulo Theories and Optimization Problems. In Theory and Applications of Satisfiability Testing - SAT 2006. Springer Berlin Heidelberg, 156--169. doi: 10.1007/11814948_18 Barrett, 2018. 2018. Satisfiability Modulo Theories), a well-studied generalisation of SAT problems for which highly optimised solvers exist Moura, 2008. 2008. Z3: An Efficient SMT Solver. In Tools and Algorithms for the Construction and Analysis of Systems. Springer Berlin Heidelberg, 337--340. doi: 10.1007/978-3-540-78800-3_24 Sebast., 2015. 2015. OptiMathSAT: A Tool for Optimization Modulo Theories. In Computer Aided Verification. Springer International Publishing, 447--454. doi: 10.1007/978-3-319-21690-4_27. A class of cost functions for which the SMT encoding of the optimisation problem becomes particularly simple are local cost functions:
A cost function on graphs is local if for all rewrites there is a cost such that for all graphs that applies to
The cost of a rewrite also immediately defines a cost to the event that defines . We can thus associate a cost with each event , given by the cost of any of the rewrites that defines.
An instance of such a local cost function often used in the context of the optimisation of computation graphs are functions of the type
for some vertex weight function – i.e. cost functions that can be expressed as sums over the costs associated to individual vertices in 3. Indeed, it is easy to see that in this case we can write
where and are the vertex deletion set and replacement graph of respectively.
As discussed in section 2.2, many of the most widely used cost functions in quantum compilation are local, as the cost of a quantum computation is often estimated by the required number of instances of the most expensive gate type (such as \texttt{CX} gates on noisy devices, or \texttt{T} gates for hardware with built-in fault tolerance protocols).
In these cases, the cost function is integer valued and the extraction problem is indeed often sparse:
The local cost function is said to be -sparse on if
In case of -sparse local cost functions, the SAT problem on can be simplified to only include
by repeatedly applying the following constraint simplification rules on any such that :
- for every parent and child , remove the parent-child constraints between and and between and . Insert in their place a parent-child constraint between and .
- for every non-compatible sibling event , remove the compatibility constraint between and . Insert in its place a compatibility constraint between and for all .
This reduces the SAT or SMT problem to a problem with variables and at most constraints.
With the completion of this section, we have described an equivalent computation on for every step of a GTS-based optimisation problem:
- a rewrite that can be applied on a graph can be added as an event to ,
- a graph that results from a sequence of rewrites can be recovered from
using
FlattenHistory, - the set of all graphs reachable from events in can be expressed as a SAT problem; depending on the cost function, the optimisation over that space can then take the form of an SMT problem.
In essence, using the confluently persistent data structure we replace a naive, exhaustive search over the space of all graphs reachable in the GTS with a SAT (or SMT) problem – solvable using highly optimised dedicated solvers that could in principle handle search spaces with up to millions of possible rewrites Zulkos., 2018. 2018. Understanding and Enhancing CDCL-based SAT Solvers.
To realise this, notice that all vertices equivalent to are vertices that will be merged with . Hence, they will all be attached to the outgoing edge of at its -th outgoing endvertex. ↩︎
Hardness can be seen by considering the special case of the extraction problem in which all events are compatible and no two events have a parent-child relation: then there are no constraints on the solution space and the optimisation problem requires finding the minimum of an arbitrary oracle over inputs. ↩︎
A similar argument also applies to cost functions that sum over graph edges, as would be the case in minIR, where operations are modelled as hyperedges. ↩︎