Skip to content

Commit

Permalink
Merge pull request #12 from EarlOld/quantum-coin-game
Browse files Browse the repository at this point in the history
Quantum Coin Game
  • Loading branch information
EarlOld authored Sep 13, 2024
2 parents 963bd7f + e909e1d commit e66dfe5
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 15 deletions.
47 changes: 47 additions & 0 deletions docs/pages/examples/quantumCoinGame.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

# Example: Quantum Coin Game

The game is played with a quantum coin, which is in a superposition of heads and tails.

Alice - Quantum Computer
Bob - Opponent(Human)

heads = |0>
tails = |1>

### The rules are simple:

1. Alice places the coin in a state of her choice (heads or tails on top) in a box that
covers the coin. Even by touching the coin, it is impossible to determine which side
is up.
2. Bob reaches into the box and has the choice to flip the coin over or not. Alice cannot
see what he is doing.
3. Alice reaches into the box and performs an operation of her choice (flip or not flip).
4. The coin is uncovered and the result is read.
5. If its heads, then Quantum Computer wins. Else, Opponent(Human) wins.

### Important: Alice and Bob don't know the result of the coin until it is uncovered.


The game is implemented using quantum.js.

```typescript

import { getQuantumCoinGameResult, quantumCoinGame } from 'library';

const res = quantumCoinGame(false);
console.log(getQuantumCoinGameResult(res));

```

### Output

```
Quantum Computer wins
```

### Links

1. (Playing with a Quantum Computer)[https://www.npmjs.com/package/library]
2. (Quantum Coin Game Wiki)[https://en.wikipedia.org/wiki/Quantum_coin_flipping]
3. (Quantum Coin Game)[https://github.com/Qiskit/textbook/blob/main/notebooks/ch-demos/coin-game.ipynb]
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 - Quantum.js@0.3.3',
text: 'MIT 2024 © EarlOld - Quantum.js@0.4.1',
},
};

Expand Down
2 changes: 1 addition & 1 deletion library/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Circuit {
}

public i(qubitIndex: number): void {
this.quantumCircuit.addGate('i', -1, qubitIndex);
this.quantumCircuit.addGate('id', -1, qubitIndex);
}

public h(qubitIndex: number): void {
Expand Down
4 changes: 2 additions & 2 deletions library/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Circuit } from './circuit';
import { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC } from './utils';
import { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC, quantumCoinGame, getQuantumCoinGameResult } from './utils';

export { Circuit, optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC };
export { Circuit, optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC, quantumCoinGame, getQuantumCoinGameResult };
34 changes: 34 additions & 0 deletions library/utils/CoinGame/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Circuit } from '../../circuit';

export type UserInput = boolean;
export type Result = 0 | 1;

export const quantumCoinGame = (flip: UserInput): Result => {
// Create a new circuit
const circuit = new Circuit(1);

// 1. Prepare the qubit
circuit.h(0);

// 2. Apply the user input
if (flip) {
circuit.x(0);
} else {
circuit.i(0);
}

// 3. Set H gate to the qubit

circuit.h(0);

// 4. Measure the qubit

circuit.run();
const result = circuit.measure(0) as number;

return result as Result;
};

export const getQuantumCoinGameResult = (result: Result): string => {
return result === 0 ? 'Quantum Computer wins!' : 'You win!';
}
4 changes: 3 additions & 1 deletion library/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { optimizeQAOAWithCOBYLA } from './QAOA';
import { sendTwoBitsWithSDC } from './SuperDenseCoding';
import { quantumCoinGame, getQuantumCoinGameResult } from './CoinGame';

export { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC };

export { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC, quantumCoinGame, getQuantumCoinGameResult };
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,6 +1,6 @@
{
"name": "@earlold/quantum.js",
"version": "0.4.0",
"version": "0.4.1",
"description": "Quantum.js is a library for quantum computing",
"main": "index.js",
"repository": "https://github.com/EarlOld/quantum.js",
Expand Down
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { sendTwoBitsWithSDC } from 'library';
import { circuitToImage } from './node/circuitToImage';

const { data, circuit } = sendTwoBitsWithSDC('11');

circuitToImage(circuit);

import { getQuantumCoinGameResult, quantumCoinGame } from 'library';

const res = quantumCoinGame(false);
console.log(getQuantumCoinGameResult(res));

0 comments on commit e66dfe5

Please sign in to comment.