Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/redfx-quantum/strange

Quantum Computing API for Java
https://github.com/redfx-quantum/strange

Last synced: about 2 months ago
JSON representation

Quantum Computing API for Java

Awesome Lists containing this project

README

        

# strange
[![Build](https://github.com/redfx-quantum/strange/actions/workflows/build.yml/badge.svg)](https://github.com/redfx-quantum/strange/actions/workflows/build.yml)
[![Maven Central](https://img.shields.io/maven-central/v/org.redfx/strange)](https://search.maven.org/#search|ga|1|org.redfx.strange)
[![License](https://img.shields.io/github/license/redfx-quantum/strange)](https://opensource.org/licenses/GPL-3.0)
[![javadoc](https://javadoc.io/badge2/org.redfx/strange/javadoc.svg)](https://javadoc.io/doc/org.redfx/strange)

# Quantum Computing API and simulator for Java

This project defines a Java API that can be used to create Quantum Programs.
A Quantum Program, defined by org.redfx.strange.Program can be executed on an implementation of the
org.redfx.strange.QuantumExecutionEnvironment.

You can read more about Java and QuantumComputing in [Quantum Computing in Action](https://www.manning.com/books/quantum-computing-in-action?a_aid=quantumjava&a_bid=e5166ab9)

![qcj](https://github.com/redfx-quantum/strange/assets/767876/324f682d-eaaf-4829-b411-4ab2abc104cc)

# Getting Started

Strange is distributed via Maven Central and can thus easily be used leveraging maven or gradle build software.

## Using JBang

JBang makes it easy to run simple Java applications without setup hassle.

You can download JBang from jbang.dev.

The following code is a very simple Java application that is using Strange (and StrangeFX). Save this code in a
file named ShortStrangeDemo.java and execute it by typing the following command in a terminal:
`jbang ShortStrangeDemo.java`

```
//DEPS org.redfx:strangefx:0.1.4
import org.redfx.strange.*;
import org.redfx.strange.gate.*;
import org.redfx.strange.local.SimpleQuantumExecutionEnvironment;
import org.redfx.strangefx.render.Renderer;
import java.util.Arrays;

public class ShortStrangeDemo {
public static void main(String[] args) {
Program p = new Program(2, new Step(new X(0)), new Step(new Hadamard(0), new X(1)));
SimpleQuantumExecutionEnvironment sqee = new SimpleQuantumExecutionEnvironment();
Qubit[] qubits = sqee.runProgram(p).getQubits();
Renderer.renderProgram(p);
Arrays.asList(qubits).forEach(q -> System.out.println("qubit with probability on 1 = "+q.getProbability()+", measured it gives "+ q.measure()));
}
}
```

The result of this is some output to the terminal, and a Window showing the Quantum Circuit you created:
```
qubit with probability on 1 = 0.4999999701976776, measured it gives 1
qubit with probability on 1 = 0.9999999403953552, measured it gives 1

```
(note that the first qubit can be measured as `0` or as `1`)
![demo output](assets/shortdemo.png)

## Using maven

A typical `pom.xml` file looks as follows:

```maven

4.0.0
org.redfx.javaqc
simplestrangedemo
jar
1.0-SNAPSHOT
SimpleStrangeDemo
http://maven.apache.org



org.redfx
strange
0.1.1





org.openjfx
javafx-maven-plugin
0.0.6

SimpleStrangeDemo



```

## Using gradle

A typical build.gradle file looks as follows:
```gradle
plugins {
id 'application'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.redfx:strange:0.1.1'
}

mainClassName = 'SimpleStrangeDemo'

```

# About the sample application.

The code pasted above in the `ShortStrangeDemo` snippet is a short version of the code
below. Both applications are similar, but the code below is more verbose which makes it
easier to explain what is going on.

```java
import org.redfx.strange.*;
import org.redfx.strange.gate.*;
import org.redfx.strange.local.SimpleQuantumExecutionEnvironment;
import java.util.Arrays;

public class SimpleStrangeDemo {

public static void main(String[] args) {
Program p = new Program(2);
Gate xGate1 = new X(0);
Step step1 = new Step();
step1.addGate(xGate1);
p.addStep(step1);
Gate hGate2 = new Hadamard(0);
Gate xGate2 = new X(1);
Step step2 = new Step();
step2.addGates(hGate2, xGate2);
p.addStep(step2);
SimpleQuantumExecutionEnvironment sqee = new SimpleQuantumExecutionEnvironment();
Result res = sqee.runProgram(p);
Qubit[] qubits = res.getQubits();
Arrays.asList(qubits).forEach(q -> System.out.println("qubit with probability on 1 = "+q.getProbability()+", measured it gives "+ q.measure()));
}

}
```

This sample create a Program that requires 2 qubits. It will create 2 steps (s and t).
The first step adds a Paul-X (NOT) Gate to the first qubit.
The second steps adds a Hadamard Gate to the first qubit, and a NOT gate to the second qubit.
Both steps are added to the Program.

In order to "run" this program, we need a QuantumExecutionEnvironment. Strange comes with a
SimpleQuantumExecutionEnvironment which contains a very simple, non-optimized quantum computer simulator.

After running the program on this simulator, we inspect the state of the Qubits. As expected, there is a 50% chance the first qubit (which had an X and an H gate) will be in the 0 state, and a 50% chance it will be in the 1 state. The second qubit will always be in the 1 state.

Running this application a number of times will consistently give the same probabilities, and different measurements.

# Visualization

The Strange API's allow creating and simulate quantum programs. A companion project, [StrangeFX](https://github.com/redfx-quantum/strangefx) , allows visualising programs, and create them with a simple drag and drop interface. The sample program above rendered via StrangeFX looks as follows:
![StrangeFX rendering](https://github.com/redfx-quantum/strangefx/blob/master/docs/images/simpleview.png)

# More samples
You can find more samples at https://github.com/johanvos/quantumjava