Skip to content

Commit

Permalink
Updates for 0.38.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Classiq Bot committed Mar 17, 2024
1 parent a49090e commit 4751c84
Show file tree
Hide file tree
Showing 277 changed files with 72,755 additions and 28,374 deletions.
313 changes: 216 additions & 97 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,168 @@
<div align="left">
[![Version](https://badge.fury.io/py/classiq.svg)](https://badge.fury.io/py/classiq)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/classiq)

<div align="center">
<img src="README_resources/classiq-logo.svg" width="300" height="150">
</div>

# Classiq

Your entry-point for creating & running quantum programs.

This repository holds a wide collection of quantum functions, algorithms, applications and tutorials built with Classiq.

<hr> <br>

<p align="center">
&emsp;
<a href="https://platform.classiq.io/">⚛️ Platform</a>
&emsp;|&emsp;
<a href="https://short.classiq.io/join-slack">👋 Join Slack</a>
&emsp;|&emsp;
<a href="https://docs.classiq.io/latest/user-guide/">📖 Documentation</a>
&emsp; | &emsp;
<a href="https://docs.classiq.io/latest/getting-started/">Getting Started</a>
&emsp;
</p>

<hr>

# Installation

Working with Classiq's latest GUI requires no installations!
Just head over to [Classiq's platform](https://platform.classiq.io/) and follow the examples below over there :)

If you'd rather work programmatically, using Python, Classiq also provides an SDK, which can be installed as follows:

```bash
pip install classiq
```

## Running This Repository's Demos

This repository has 2 kinds of demos: `.qmod` and `.ipynb`.

The `.qmod` files are intended for usage with [Classiq's platform](https://platform.classiq.io/).
Upload those `.qmod` files into the [Synthesis tab](https://platform.classiq.io/synthesis)

The `.ipynb` files are intended to be viewed inside [JupyterLab](https://jupyter.org/).

# Create Quantum Programs with Classiq

The **Classiq** platform is all you need for creating any quantum program. Read more for the details.
The simplest quantum circuit has 1 qubit, and has a single `X` gate.

Using Classiq's SDK, it would like like so:

```python
from classiq import *

NUM_QUBITS = 1


@qfunc
def main(res: Output[QBit]):
allocate(NUM_QUBITS, res)
X(res)


model = create_model(main)
quantum_program = synthesize(model)

show(quantum_program)

result = execute(quantum_program).result()
print(result[0].value.parsed_counts)
# [{'res': 1.0}: 1000]
```

Let's unravel the code above:

1. `def main` : We define the logic of our quantum program. We'll expand on this point soon below.
2. `create_model` : We convert the logic we defined into a Model.
3. `synthesize` : We synthesize the Model into a Quantum Program. From a logical definition of quantum operations, into a series of quantum gates.
4. `execute` : Executing the quantum program. Can be executed on a physical quantum computer, or on simulations.

## 1) Defining the Logic of Quantum Programs

The function above had 4 lines:

```python
@qfunc
def main(res: Output[QBit]):
allocate(NUM_QUBITS, res)
X(res)
```

The 1st line states that the function will be a quantum one. [Further documentation](https://docs.classiq.io/latest/user-guide/platform/qmod/python/functions/).

The 2nd line defines the type of the output. [Further examples on types](https://docs.classiq.io/latest/user-guide/platform/qmod/python/types/)

The 3rd line allocates several qubits (in this example, only 1) in this quantum variable. [Further details on allocate](https://docs.classiq.io/latest/user-guide/platform/qmod/python/quantum-expressions/)

The 4th line applies an `X` operator on the quantum variable. [Further details on quantum operators](https://docs.classiq.io/latest/user-guide/platform/qmod/python/operators/)

### More Examples

Initializing $\ket{-}$ state:

```python
@qfunc
def prep_minus(out: Output[QBit]) -> None:
allocate(1, out)
X(out)
H(out)
```

A part of the Deutsch Jozsa algorithm (see the full algirthm [here](/algorithms/deutsch_josza/deutsch_jozsa.ipynb))

```python
@qfunc
def deutsch_jozsa(predicate: QCallable[QNum, QBit], x: QNum) -> None:
hadamard_transform(x)
my_oracle(predicate=lambda x, y: predicate(x, y), target=x)
hadamard_transform(x)
```

A part of a QML encoder (see the full algirthm [here](/algorithms/qml/quantum_autoencoder/quantum_autoencoder.ipynb))

```python
@qfunc
def angle_encoding(
exe_params: QParam[List[float]], qbv: Output[QArray[QBit, "len(exe_params)"]]
) -> None:
allocate(exe_params.len(), qbv)
repeat(
count=exe_params.len(),
iteration=lambda index: RY(pi * exe_params[index], qbv[index]),
)
```

For more, see this repository :)

## 2) Logic to Models

As we saw above, the `main` function can be converted to a model using `model = create_model(main)`.

A model is built out of 2 parts: a `qmod`, and `synthesis options`.
The former is a quantum language used for defining quantum programs, while the latter is a configuration for the execution of the program.

The model can be saved via `write_qmod(model, "file_name")`, which will save 2 files: `file_name.qmod` and `file_name.synthesis_options.json`.
You may encounter these files in this repository.

## 3) Synthesis : Models to Quantum Program

This is where the magic happens.
Taking a model, which is a set of logical operations, and synthesizing it into physical qubits and the gates entangling them, is not an easy task.

Classiq's synthesis engine is able to optimize this process, whether by requiring the minimal amount of physical qubits, thus reusing as many qubits as possible, or by requiring minimal circuit width, thus lowering execution time and possible errors.

## 4) Execution

Classiq provides an easy-to-use way to execute quantum programs, and provides various insights of the execution results.

## Diagrams

1 diagram is worth a thousand words

```mermaid
flowchart
Expand All @@ -22,8 +180,6 @@ flowchart
Analyze[<a href='https://docs.classiq.io/latest/user-guide/platform/analyzer/'>Analyze & Debug</a>]
ExternalProgram[<a href='https://docs.classiq.io/latest/user-guide/platform/executor/alternative-formats/'>Any Format, e.g QASM</a>]
IBM[IBM]
Amazon[Amazon Braket]
Azure[Azure Quantum]
Expand All @@ -41,81 +197,91 @@ flowchart
Execution --> Amazon
Execution --> Azure
Execution --> Nvidia
```

# Build Your Own

With Classiq, you can build anything. Classiq provides a powerful modeling language to describe any quantum program, which can then be synthesized and executed on any hardware or simulator. Explore our [Documentation](https://docs.classiq.io/latest/user-guide/) to learn everything.

## SDK : Classiq's Python Interface

```

<hr>
<br>
### Example: 3+5 with Classiq

<p align="center">
&emsp;
<a href="https://short.classiq.io/join-slack">⚛️ Platform</a>
&emsp;|&emsp;
<a href="https://short.classiq.io/join-slack">👋 Join Slack</a>
&emsp;|&emsp;
<a href="https://docs.classiq.io/latest/user-guide/">📖 Documentation</a>
&emsp; | &emsp;
<a href="https://docs.classiq.io/latest/getting-started/">Getting Started</a>
&emsp;
</p>
```python
from classiq import (
QArray,
Output,
allocate,
qfunc,
X,
QNum,
synthesize,
create_model,
show,
execute,
)

<hr>

## Classiq's Github Repository
@qfunc
def get_3(x: Output[QArray]) -> None:
allocate(2, x)
X(x[0])
X(x[1])

You can find a wide collection of quantum functions, algorithms, applications and tutorials built with Classiq.

Usage Example - Portfolio Optimization:
@qfunc
def get_5(x: Output[QArray]) -> None:
allocate(3, x)
X(x[0])
X(x[2])

Go to the Portfolio optimization directory: `applications/finance/portfolio_optimizaition`

### In the IDE:
@qfunc
def main(res: Output[QNum]) -> None:
a = QNum("a")
b = QNum("b")
get_3(a)
get_5(b)
res |= a + b # should be 8

1. Sign up to the <a href='https://platform.classiq.io/'>classiq platform</a>
2. Upload the `.qmod` file to the <a href='https://platform.classiq.io/synthesis'>synthesis tab</a>

### In the SDK:
model = create_model(main)
quantum_program = synthesize(model)

1. Start with downloading Classiq:
show(quantum_program)

```bash
pip install -U classiq
result = execute(quantum_program).result()
print(result[0].value.parsed_counts)
```

2. Download the `.ipynb` file
3. Run the notebook in your preferred environment

<hr>
## IDE : Classiq's Platform

## Build Your Own
The examples found in this repository can be accessed via [Classiq's platform](https://platform.classiq.io/), in the [`model`](https://platform.classiq.io/dsl-synthesis) tab, under the same folder structure.

With Classiq, you can build anything. Classiq provides a powerful modeling language to describe any quantum program, which can then be synthesized and executed on any hardware or simulator. Explore our <a href="https://docs.classiq.io/latest/user-guide/">Documentation</a> to learn everything.
Additionally, one may write his own model in the model editor (highlighted in green) or upload his own model (highlighted in red)

### 3+5 with Classiq
![writing_models.png](README_resources/writing_models.png)

### In the IDE:
### Example: 3+5 with Classiq

1. Create a model (paste in the <a href="https://platform.classiq.io/dsl-synthesis">`model`</a> tab)
1. Create a model (paste in the [`model`](https://platform.classiq.io/dsl-synthesis) tab)

```
qfunc get_3(output x: qbit[2]){
qfunc get_3(output x: qnum){
allocate<2>(x);
X(x[0:1]);
X(x[1:2]);
X(x[0]);
X(x[1]);
}
qfunc get_5(output x: qbit[3]){
qfunc get_5(output x: qnum){
allocate<3>(x);
X(x[0:1]);
X(x[2:3]);
X(x[0]);
X(x[2]);
}
qfunc main(output res: qbit[4]){
a: qbit[2];
b: qbit[3];
qfunc main(output res: qnum){
a: qnum;
b: qnum;
get_3(a);
get_5(b);
res = a + b;
Expand Down Expand Up @@ -150,54 +316,7 @@ qfunc main(output res: qbit[4]){

</center>

### In the SDK:

```python
from classiq import (
QArray,
Output,
allocate,
QFunc,
X,
QNum,
synthesize,
create_model,
show,
execute,
)


@QFunc
def get_3(x: Output[QArray]) -> None:
allocate(2, x)
X(x[0])
X(x[1])


@QFunc
def get_5(x: Output[QArray]) -> None:
allocate(3, x)
X(x[0])
X(x[2])


@QFunc
def main(res: Output[QNum]) -> None:
a = QNum("a")
b = QNum("b")
get_3(a)
get_5(b)
res |= a + b # should be 8


qprog = synthesize(create_model(main))

show(qprog)
result = execute(qprog).result()
print(result[0].value.parsed_counts)
```

<hr>

Have questions? Feedback? Something to share?
Welcome to join our open <a href="https://short.classiq.io/join-slack">Slack Community</a>
Welcome to join our open [Slack Community](https://short.classiq.io/join-slack)
Binary file added README_resources/writing_models.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4751c84

Please sign in to comment.