From 151231a67475d9010e0e4a622d09687cab6198b2 Mon Sep 17 00:00:00 2001 From: Sapozhnyk Dmytro Date: Fri, 9 Aug 2024 00:58:04 +0300 Subject: [PATCH] Added Bell States --- README.md | 10 +++ docs/pages/examples/bellStates.mdx | 101 +++++++++++++++++++++++++++++ docs/theme.config.tsx | 2 +- library/__tests__/circuit.test.ts | 43 ++++++++++++ library/circuit.ts | 52 +++++++++++++++ library/types/index.ts | 11 ++++ package-lock.json | 4 +- package.json | 2 +- src/index.ts | 22 ++----- 9 files changed, 226 insertions(+), 21 deletions(-) create mode 100644 docs/pages/examples/bellStates.mdx create mode 100644 library/types/index.ts diff --git a/README.md b/README.md index af91bbf..97644f9 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/docs/pages/examples/bellStates.mdx b/docs/pages/examples/bellStates.mdx new file mode 100644 index 0000000..28a4139 --- /dev/null +++ b/docs/pages/examples/bellStates.mdx @@ -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% +``` + + + diff --git a/docs/theme.config.tsx b/docs/theme.config.tsx index 612ef4a..03e9e4a 100644 --- a/docs/theme.config.tsx +++ b/docs/theme.config.tsx @@ -11,7 +11,7 @@ const config: DocsThemeConfig = { }, docsRepositoryBase: 'https://github.com/EarlOld/quantum.js', footer: { - text: 'MIT 2024 © EarlOld - Quantum.js@0.3.1', + text: 'MIT 2024 © EarlOld - Quantum.js@0.3.2', }, }; diff --git a/library/__tests__/circuit.test.ts b/library/__tests__/circuit.test.ts index 031a4f7..7fb581f 100644 --- a/library/__tests__/circuit.test.ts +++ b/library/__tests__/circuit.test.ts @@ -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'); + }); + + }); diff --git a/library/circuit.ts b/library/circuit.ts index be19e10..d9a0348 100644 --- a/library/circuit.ts +++ b/library/circuit.ts @@ -1,5 +1,7 @@ import QuantumCircuit from 'quantum-circuit'; +import { ObjectQubitState } from './types'; + export class Circuit { private quantumCircuit: QuantumCircuit; @@ -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); } @@ -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 diff --git a/library/types/index.ts b/library/types/index.ts new file mode 100644 index 0000000..578d3e7 --- /dev/null +++ b/library/types/index.ts @@ -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; +}; diff --git a/package-lock.json b/package-lock.json index 1cd56f4..9b8f4c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@earlold/quantum.js", - "version": "0.3.0", + "version": "0.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@earlold/quantum.js", - "version": "0.3.0", + "version": "0.3.2", "license": "ISC", "dependencies": { "mathjs": "^13.0.3", diff --git a/package.json b/package.json index 09d078b..2327de5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 93e0752..12f904e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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());