Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor compiler infrastructure #42

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@ name: CI

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]
workflow_dispatch:

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[test]
- name: Run unittest
run: |
bash tests/test.sh
- uses: actions/checkout@v4
- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install git+ssh://[email protected]/OpenQuantumDesign/compiler_infrastructure
pip install .[test]
- name: Run unittest
run: |
bash tests/test.sh
1 change: 1 addition & 0 deletions docs/explanation/analog_interface.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The analog interface represents a quantum experiment expressed by time evolving Hamiltonians.
Empty file.
161 changes: 80 additions & 81 deletions docs/explanation/canonicalization.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
# Visitor pattern and compilation
Canonicalization is used to remove redundancy in the representation of a program.

So far we have been working on Canonicalization using Visitor pattern. First let's see how we represent objects in our syntax.

## Abstract Syntax Tree for Operators

We represent `Operator` as trees like:
/// admonition | Operator

In our language we represent all operators as abstract syntax trees. this the operator
` PauliX() @ PauliX()`
would be reprsented as the tree:

```mermaid
graph TD;
A[@] --> B["PauliX()"]
A --> C["PauliX()"]
```

///

In Canonicalization we ensure that for operators:
Consider the following two Hamiltonians:

$$
H_{1} = X \otimes I + I \otimes X
Expand All @@ -35,28 +16,21 @@ $$
H_{c} = 1\cdot(I \otimes X) + 1\cdot(X \otimes I)
$$

These canonicalization steps are done using `Visitors` and `Transformers`. For every kind of canonicalization operation (like for example `distribution`) we define a graph like:

```mermaid
stateDiagram-v2

[*] --> verifier: enter
verifier --> transformer: fail
transformer --> verifier: done
verifier --> terminal: pass
```

Now we describe all the visitors and transformers we have.
These canonicalization steps (e.g. distribution) are done by implementing a `RewritesRule` with the corresponding logic.

## Visitors and Transformers
## Canonicalization Rules

### Distribution

This distributes oeprators and an example can be the conversion:
[`Distribution`][midstack.compiler.analog.rewrite.canonicalize.OperatorDistribute] distributes the multiplication, scalar multiplication and tensor product of operators over the addition of operators.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$X \otimes (Y + Z) \longrightarrow X \otimes Y + X \otimes Z$$

/// tab | Original Graph
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -76,9 +50,9 @@ $$X \otimes (Y + Z) \longrightarrow X \otimes Y + X \otimes Z$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -101,14 +75,19 @@ $$X \otimes (Y + Z) \longrightarrow X \otimes Y + X \otimes Z$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Gather Math Expression

This gathers math expressions of oprators and an example can be the conversion:
[`GatherMath`][midstack.compiler.analog.rewrite.canonicalize.GatherMathExpr] centralizes the coefficients of the operators by gathering them.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ X \times 3 \times I \longrightarrow 3 \times (X \times Y)$$
/// tab | Original Graph
$$ X \times 3 \times I \longrightarrow 3 \times (X \times I)$$
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -128,9 +107,9 @@ $$ X \times 3 \times I \longrightarrow 3 \times (X \times Y)$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -150,14 +129,20 @@ $$ X \times 3 \times I \longrightarrow 3 \times (X \times Y)$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Proper Order

This converts the operator to a proper order like:
[`ProperOrder`][midstack.compiler.analog.rewrite.canonicalize.ProperOrder] takes a chain of OperatorMul or a chain of OperatorAdd and puts the operation order from left to right.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ X \otimes (Y \otimes Z) \longrightarrow (X \otimes Y) \otimes Z $$
/// tab | Original Graph

//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -177,9 +162,9 @@ $$ X \otimes (Y \otimes Z) \longrightarrow (X \otimes Y) \otimes Z $$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -199,14 +184,19 @@ $$ X \otimes (Y \otimes Z) \longrightarrow (X \otimes Y) \otimes Z $$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Pauli Algebra

This converts the operator to a proper order like:
[`PauliAlgebra`][midstack.compiler.analog.rewrite.canonicalize.PauliAlgebra] applies the Pauli algebra to simplify the operator.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ X \times Y + I \times I \longrightarrow iZ + I $$
/// tab | Original Graph
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -229,9 +219,9 @@ $$ X \times Y + I \times I \longrightarrow iZ + I $$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -251,14 +241,19 @@ $$ X \times Y + I \times I \longrightarrow iZ + I $$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Normal Order

This converts the operator to the Normal order form like:
[`NormalOrder`][midstack.compiler.analog.rewrite.canonicalize.NormalOrder] puts the ladder operators into normal order.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ C \times A + A \times C \longrightarrow C \times A + C \times A + J$$
/// tab | Original Graph
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -281,9 +276,9 @@ $$ C \times A + A \times C \longrightarrow C \times A + C \times A + J$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -309,14 +304,19 @@ $$ C \times A + A \times C \longrightarrow C \times A + C \times A + J$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Prune Identity

This removed unnecessary Identities from the graph. Note that this only affects ladders as identities are already removed from the graph for paulis using pauli algebra.
[`PruneIdentity`][midstack.compiler.analog.rewrite.canonicalize.PruneIdentity] prunes the unnecessary ladder identities from the graph.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ C\times A \times J\longrightarrow C \times A$$
/// tab | Original Graph
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -336,9 +336,9 @@ $$ C\times A \times J\longrightarrow C \times A$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -355,14 +355,19 @@ $$ C\times A \times J\longrightarrow C \times A$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Sorted Order

This sorts the terms in addition like:
[`SortedOrder`][midstack.compiler.analog.rewrite.canonicalize.SortedOrder] sorts the addition terms in operators into a predefined order.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ X \otimes I + I \otimes X \longrightarrow I \otimes X + X \otimes I$$
/// tab | Original Graph
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -385,9 +390,9 @@ $$ X \otimes I + I \otimes X \longrightarrow I \otimes X + X \otimes I$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand All @@ -410,14 +415,19 @@ $$ X \otimes I + I \otimes X \longrightarrow I \otimes X + X \otimes I$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

////
///

### Scale Terms

This just scales the terms in the additions for consistency:
[`ScaleTerms`][midstack.compiler.analog.rewrite.canonicalize.ScaleTerms] introduces scalar multiplication to terms without a coefficient for a more consistent reprensentation.

<!-- prettier-ignore -->
/// admonition | Example
type: example

$$ I \otimes X + X \otimes I \longrightarrow 1*(I \otimes X) + 1*(X \otimes I)$$
/// tab | Original Graph
//// tab | Original Graph

```mermaid
graph TD
Expand All @@ -440,9 +450,9 @@ $$ I \otimes X + X \otimes I \longrightarrow 1*(I \otimes X) + 1*(X \otimes I)$$
classDef MathExpr stroke:#800000,stroke-width:3px
```

///
////

/// tab | Transformed Graph
//// tab | Transformed Graph

```mermaid
graph TD
Expand Down Expand Up @@ -471,22 +481,11 @@ classDef OperatorMul stroke:#800000,stroke-width:3px
classDef MathExpr stroke:#800000,stroke-width:3px
```

///

## Composition of Visitors

The visitors are composed in a graphical structure. Recall that or every kind of canonicalization operation (like for example `distribution`) we define a graph like:
////

```mermaid
stateDiagram-v2

[*] --> verifier: enter
verifier --> transformer: fail
transformer --> verifier: done
verifier --> terminal: pass
```
///

Hence, using this, we define the full graphical structure as:
## Canonicalization Pass

```mermaid
stateDiagram-v2
Expand Down
Loading
Loading