Skip to content

Commit

Permalink
block system
Browse files Browse the repository at this point in the history
  • Loading branch information
sungwoo-pasqal committed Jan 17, 2025
1 parent 7d842db commit 5c65029
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
56 changes: 24 additions & 32 deletions docs/transition.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,28 @@

### Transition Consideration
## Transition Consideration
This document is for Qadence users to use Qadence2 funcationalities with Qadence format.
The examples in Qadence Contents and Tutorials will be presented with qadence2.legacy.
For Qadence2 code manual, please refer to ~~.
The examples in Qadence Contents and Tutorials will be presented with qadence2.expressions.legacy package. The provided Operators and functions are states as below
For Qadence2 code details, please refer to https://github.com/pasqal-io/qadence2-core.

Qadence Contents
- Block System
- Parametric Programs
- Quantum Models
- Quantum Registers
- State Initializaton
- Arbitary Hamiltonians
- Time-dependent Generators
- QML Constructors
- Wavefunction Overlaps
- Backends

Qadence Tutorials
- Digital-Analog Quantum Computation
- Basic operations on neutral-atoms ...


# Block System

## Primitive blocks
### Operators

Qadence
```python exec="on" source="material-block" html="1" session="getting_started"
from qadence import chain, RX, CNOT
from qadence import RX, CNOT

rx = RX(0, 0.5)
cnot = CNOT(0, 1)

block = chain(rx, cnot)
```

Qadence2
```python exec="on" source="material-block" html="1" session="getting_started"
from qadence2.extensions.legacy import chain, RX, CNOT
from qadence2.extensions.legacy import RX, CNOT

rx = RX(0, 0.5)
cnot = CNOT(0, 1)

block = chain(rx, cnot)
```

## Composite blocks
### Block System

Qadence
```python exec="on" source="material-block" html="1" session="getting_started"
Expand All @@ -67,6 +44,7 @@ chain_1 = chain(X(1), Y(1))
kron_block = kron(chain_0, chain_1)
```

### Compose Functions

Qadence
```python exec="on" source="material-block" html="1" session="getting_started"
Expand All @@ -85,23 +63,37 @@ Qadence2
from qadence2.extensions.legacy import X, Y, add

def xy_int(i: int, j: int):
return (1/2) * (X(i)@X(j) + Y(i)@Y(j))
return (1/2) * (X(i)*X(j) + Y(i)*Y(j))

n_qubits = 3

xy_ham = add(xy_int(i, i+1) for i in range(n_qubits-1))
```

## Next session
### Quantum Fourier Transform Example

Qadence
```python exec="on" source="material-block" html="1" session="getting_started"
from qadence import H, CPHASE, PI, chain, kron

def qft_layer(qs: tuple, l: int):
cphases = chain(CPHASE(qs[j], qs[l], PI/2**(j-l)) for j in range(l+1, len(qs)))
return H(qs[l]) * cphases

def qft(qs: tuple):
return chain(qft_layer(qs, l) for l in range(len(qs)))
```

Qadence2
```python exec="on" source="material-block" html="1" session="getting_started"
from qadence2.extensions.legacy import H, CPHASE, PI, chain, kron

def qft_layer(qs: tuple, l: int):
cphases = chain(CPHASE(qs[j], qs[l], PI/2**(j-l)) for j in range(l+1, len(qs)))
return H(qs[l]) * cphases

def qft(qs: tuple):
return chain(qft_layer(qs, l) for l in range(len(qs)))
```

## Next session
Expand Down
11 changes: 8 additions & 3 deletions qadence2/extensions/legacy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from qadence2_expressions.operators import CZ, X, Y, Z
from qadence2_expressions.operators import CZ, X, Y, Z, H

from .model import QuantumModel
from .operators import CNOT, RX, RY, RZ, N
from .utils import add, chain, kron, mul, pow
from .operators import CNOT, RX, RY, RZ, N, T, PHASE, CPHASE
from .utils import PI, add, chain, kron, mul, pow

__all__ = [
"QuantumModel",
Expand All @@ -22,4 +22,9 @@
"Z",
"CZ",
"N",
"PI",
"H",
"T",
"PHASE",
"CPHASE",
]
19 changes: 18 additions & 1 deletion qadence2/extensions/legacy/operators.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

import qadence2_expressions.operators as ops
from qadence2_expressions.core.constructors import promote, unitary_hermitian_operator, parametric_operator
from qadence2_expressions.operators import _join_rotation
from qadence2_expressions import variable
from qadence2_expressions.core.expression import Expression

from .utils import PI

# The N = (1/2)(I-Z) operator
N = ops.Z1


def CNOT(target: int, control: int) -> Expression:
def CNOT(control: int, target: int) -> Expression:
return ops.NOT(target=(target,), control=(control,))


Expand All @@ -29,3 +33,16 @@ def _get_variable(expr: Expression | str | float) -> Expression:
return variable(expr)
if isinstance(expr, (Expression, float)):
return expr


def T(target: int) -> Expression:
return unitary_hermitian_operator("T")(target=(target,))


def PHASE(target: int, parameters: Expression | str | float) -> Expression:
return parametric_operator("PHASE", promote(_get_variable(parameters)), join=_join_rotation)(target=(target,))


def CPHASE(control: int, target: int, parameters: Expression | str | float) -> Expression:
return parametric_operator("CPHASE", promote(_get_variable(parameters)), join=_join_rotation)(target=(target,), control=(control,))

3 changes: 3 additions & 0 deletions qadence2/extensions/legacy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from enum import Enum, auto
from typing import Generator

import torch
from qadence2_expressions.core.expression import Expression

PI = torch.pi


class ParadigmStrategy(Enum):
DIGITAL = auto()
Expand Down

0 comments on commit 5c65029

Please sign in to comment.