Skip to content

Commit

Permalink
Merge pull request #9 from EarlOld/bell-states
Browse files Browse the repository at this point in the history
Added Bell States
  • Loading branch information
EarlOld authored Aug 8, 2024
2 parents daec31a + 151231a commit 8bad3a1
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 21 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ The library supports the following quantum gates, check the table below for the
| Circuit.teleportationOneToTree | Teleportation of a qubit from one quantum register to another |
| optimizeQAOAWithCOBYLA | Optimize the QAOA algorithm using the COBYLA optimization algorithm for Max-Cut problem |


## Bell State Functions

| Functions | Description |
| ------------------------ | --------------------------------------------------------------------------- |
| Circuit.prepareBellPhiPlus | Prepare the Bell state Phi+ |
| Circuit.prepareBellPhiMinus | Prepare the Bell state Phi- |
| Circuit.prepareBellPsiPlus | Prepare the Bell state Psi+ |
| Circuit.prepareBellPsiMinus | Prepare the Bell state Psi- |

## Dependencies

The library uses the following dependencies:
Expand Down
101 changes: 101 additions & 0 deletions docs/pages/examples/bellStates.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Bell States

## Description

The Bell states are four specific maximally entangled quantum states of two qubits. They are in a superposition of 0 and 1, and are the maximally entangled states. The Bell states form an orthonormal basis for the two qubits, and when one qubit is measured, the outcome of the other qubit is immediately known, no matter how far apart the qubits are.

## Parameters

- `first` (Type: `number`) - The first qubit index.
- `second` (Type: `number`) - The second qubit index.

## Example 1 - Prepare Bell Phi Plus State

### Code

```typescript
import { Circuit } from '@earlold/quantum.js';

const circuit = new Circuit(2);
circuit.prepareBellPhiPlus(0, 1);
circuit.run();
console.log(circuit.stateToString);
```

### Output

```console
0.00000000+0.00000000i|00> 0.00000%
0.70710678+0.00000000i|01> 50.00000%
0.70710678+0.00000000i|10> 50.00000%
0.00000000+0.00000000i|11> 0.00000%
```

## Example 2 - Prepare Bell Phi Minus State

### Code

```typescript
import { Circuit } from '@earlold/quantum.js';

const circuit = new Circuit(2);
circuit.prepareBellPhiMinus(0, 1);
circuit.run();
console.log(circuit.stateToString);
```

### Output

```console
0.70710678+0.00000000i|00> 50.00000%
0.00000000+0.00000000i|01> 0.00000%
0.00000000+0.00000000i|10> 0.00000%
-0.70710678+0.00000000i|11> 50.00000%
```

## Example 3 - Prepare Bell Psi Plus State

### Code

```typescript
import { Circuit } from '@earlold/quantum.js';

const circuit = new Circuit(2);
circuit.prepareBellPsiPlus(0, 1);
circuit.run();
console.log(circuit.stateToString);
```

### Output

```console
0.00000000+0.00000000i|00> 0.00000%
0.70710678+0.00000000i|01> 50.00000%
0.70710678+0.00000000i|10> 50.00000%
0.00000000+0.00000000i|11> 0.00000%
```

## Example 4 - Prepare Bell Psi Minus State

### Code

```typescript
import { Circuit } from '@earlold/quantum.js';

const circuit = new Circuit(2);
circuit.prepareBellPsiMinus(0, 1);
circuit.run();
console.log(circuit.stateToString);
```

### Output

```console
0.00000000+0.00000000i|00> 0.00000%
-0.70710678+0.00000000i|01> 50.00000%
0.70710678+0.00000000i|10> 50.00000%
0.00000000+0.00000000i|11> 0.00000%
```



2 changes: 1 addition & 1 deletion docs/theme.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const config: DocsThemeConfig = {
},
docsRepositoryBase: 'https://github.com/EarlOld/quantum.js',
footer: {
text: 'MIT 2024 © EarlOld - [email protected].1',
text: 'MIT 2024 © EarlOld - [email protected].2',
},
};

Expand Down
43 changes: 43 additions & 0 deletions library/__tests__/circuit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,47 @@ describe('Circuit', () => {

// expect(randomString.length).toEqual(10);
// });

it('should make bell state PhiPlus', () => {
const circuit = new Circuit(2);
circuit.prepareBellPhiPlus(0, 1);
circuit.run();

const state = circuit.stateToArray();
expect(state[0].chanceStr).toEqual('50.00000');
expect(state[3].chanceStr).toEqual('50.00000');
});

it('should make bell state PhiMinus', () => {
const circuit = new Circuit(2);
circuit.prepareBellPhiMinus(0, 1);
circuit.run();

const state = circuit.stateToArray();
expect(state[0].chanceStr).toEqual('50.00000');
expect(state[3].chanceStr).toEqual('50.00000');
});


it('should make bell state PsiPlus', () => {
const circuit = new Circuit(2);
circuit.prepareBellPsiPlus(0, 1);
circuit.run();

const state = circuit.stateToArray();
expect(state[1].chanceStr).toEqual('50.00000');
expect(state[2].chanceStr).toEqual('50.00000');
});

it('should make bell state PsiMinus', () => {
const circuit = new Circuit(2);
circuit.prepareBellPsiMinus(0, 1);
circuit.run();

const state = circuit.stateToArray();
expect(state[1].chanceStr).toEqual('50.00000');
expect(state[2].chanceStr).toEqual('50.00000');
});


});
52 changes: 52 additions & 0 deletions library/circuit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import QuantumCircuit from 'quantum-circuit';

import { ObjectQubitState } from './types';

export class Circuit {
private quantumCircuit: QuantumCircuit;

Expand Down Expand Up @@ -71,6 +73,14 @@ export class Circuit {
return this.quantumCircuit.measureAll();
}

public stateToString(): string {
return this.quantumCircuit.stateAsString(false) as string;
}

public stateToArray(): ObjectQubitState[] {
return this.quantumCircuit.stateAsArray(false, undefined, undefined, undefined) as ObjectQubitState[];
}

public toQsharp(): string {
return this.quantumCircuit.exportQSharp('quantum.js', false, null, null, false, null);
}
Expand All @@ -79,6 +89,48 @@ export class Circuit {
return this.quantumCircuit.exportSVG(true);
}

// Bell states

public prepareBellPhiPlus(first: number, second: number): void {
if (first === second) {
throw new Error('The qubits must be different');
}

this.h(first);
this.cx(first, second);
}

public prepareBellPhiMinus(first: number, second: number): void {
if (first === second) {
throw new Error('The qubits must be different');
}

this.h(first);
this.z(first);
this.cx(first, second);
}

public prepareBellPsiPlus(first: number, second: number): void {
if (first === second) {
throw new Error('The qubits must be different');
}

this.h(first);
this.x(second);
this.cx(first, second);
}

public prepareBellPsiMinus(first: number, second: number): void {
if (first === second) {
throw new Error('The qubits must be different');
}

this.h(first);
this.z(first);
this.x(second);
this.cx(first, second);
}

public static genRandomNumber(max: number): number {
// Generate a random number between 0 and max
// Max to binary
Expand Down
11 changes: 11 additions & 0 deletions library/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type ObjectQubitState = {
index: number;
indexBinStr: string;
amplitude: { re: number; im: number };
amplitudeStr: string;
magnitude: number;
chance: number;
chanceStr: string;
phase: number;
phaseStr: string;
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@earlold/quantum.js",
"type": "module",
"version": "0.3.1",
"version": "0.3.2",
"description": "Quantum.js is a library for quantum computing",
"main": "index.js",
"repository": "https://github.com/EarlOld/quantum.js",
Expand Down
22 changes: 5 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { optimizeQAOAWithCOBYLA } from 'library';
import { Circuit } from 'library';

const nodes = [0, 1, 2, 3, 4];
const edges: Array<[number, number]> = [
[0, 3],
[0, 4],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
];
const steps = 1;
const circuit = new Circuit(2);
circuit.prepareBellPsiMinus(0, 1);
circuit.run();

const { beta, gamma, score, maxCutScore } = optimizeQAOAWithCOBYLA(nodes, edges, steps);

console.log('Optimized beta:', beta);
console.log('Optimized gamma:', gamma);
console.log('Best score:', score);
console.log('maxCutScore:', maxCutScore);
console.log(circuit.stateToString());

0 comments on commit 8bad3a1

Please sign in to comment.