Skip to content

Commit e66dfe5

Browse files
authored
Merge pull request #12 from EarlOld/quantum-coin-game
Quantum Coin Game
2 parents 963bd7f + e909e1d commit e66dfe5

File tree

9 files changed

+94
-15
lines changed

9 files changed

+94
-15
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# Example: Quantum Coin Game
3+
4+
The game is played with a quantum coin, which is in a superposition of heads and tails.
5+
6+
Alice - Quantum Computer
7+
Bob - Opponent(Human)
8+
9+
heads = |0>
10+
tails = |1>
11+
12+
### The rules are simple:
13+
14+
1. Alice places the coin in a state of her choice (heads or tails on top) in a box that
15+
covers the coin. Even by touching the coin, it is impossible to determine which side
16+
is up.
17+
2. Bob reaches into the box and has the choice to flip the coin over or not. Alice cannot
18+
see what he is doing.
19+
3. Alice reaches into the box and performs an operation of her choice (flip or not flip).
20+
4. The coin is uncovered and the result is read.
21+
5. If its heads, then Quantum Computer wins. Else, Opponent(Human) wins.
22+
23+
### Important: Alice and Bob don't know the result of the coin until it is uncovered.
24+
25+
26+
The game is implemented using quantum.js.
27+
28+
```typescript
29+
30+
import { getQuantumCoinGameResult, quantumCoinGame } from 'library';
31+
32+
const res = quantumCoinGame(false);
33+
console.log(getQuantumCoinGameResult(res));
34+
35+
```
36+
37+
### Output
38+
39+
```
40+
Quantum Computer wins
41+
```
42+
43+
### Links
44+
45+
1. (Playing with a Quantum Computer)[https://www.npmjs.com/package/library]
46+
2. (Quantum Coin Game Wiki)[https://en.wikipedia.org/wiki/Quantum_coin_flipping]
47+
3. (Quantum Coin Game)[https://github.com/Qiskit/textbook/blob/main/notebooks/ch-demos/coin-game.ipynb]

docs/theme.config.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const config: DocsThemeConfig = {
1111
},
1212
docsRepositoryBase: 'https://github.com/EarlOld/quantum.js',
1313
footer: {
14-
text: 'MIT 2024 © EarlOld - Quantum.js@0.3.3',
14+
text: 'MIT 2024 © EarlOld - Quantum.js@0.4.1',
1515
},
1616
};
1717

library/circuit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class Circuit {
1010
}
1111

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

1616
public h(qubitIndex: number): void {

library/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { Circuit } from './circuit';
2-
import { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC } from './utils';
2+
import { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC, quantumCoinGame, getQuantumCoinGameResult } from './utils';
33

4-
export { Circuit, optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC };
4+
export { Circuit, optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC, quantumCoinGame, getQuantumCoinGameResult };

library/utils/CoinGame/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Circuit } from '../../circuit';
2+
3+
export type UserInput = boolean;
4+
export type Result = 0 | 1;
5+
6+
export const quantumCoinGame = (flip: UserInput): Result => {
7+
// Create a new circuit
8+
const circuit = new Circuit(1);
9+
10+
// 1. Prepare the qubit
11+
circuit.h(0);
12+
13+
// 2. Apply the user input
14+
if (flip) {
15+
circuit.x(0);
16+
} else {
17+
circuit.i(0);
18+
}
19+
20+
// 3. Set H gate to the qubit
21+
22+
circuit.h(0);
23+
24+
// 4. Measure the qubit
25+
26+
circuit.run();
27+
const result = circuit.measure(0) as number;
28+
29+
return result as Result;
30+
};
31+
32+
export const getQuantumCoinGameResult = (result: Result): string => {
33+
return result === 0 ? 'Quantum Computer wins!' : 'You win!';
34+
}

library/utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { optimizeQAOAWithCOBYLA } from './QAOA';
22
import { sendTwoBitsWithSDC } from './SuperDenseCoding';
3+
import { quantumCoinGame, getQuantumCoinGameResult } from './CoinGame';
34

4-
export { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC };
5+
6+
export { optimizeQAOAWithCOBYLA, sendTwoBitsWithSDC, quantumCoinGame, getQuantumCoinGameResult };

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@earlold/quantum.js",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Quantum.js is a library for quantum computing",
55
"main": "index.js",
66
"repository": "https://github.com/EarlOld/quantum.js",

src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { sendTwoBitsWithSDC } from 'library';
2-
import { circuitToImage } from './node/circuitToImage';
3-
4-
const { data, circuit } = sendTwoBitsWithSDC('11');
5-
6-
circuitToImage(circuit);
7-
1+
import { getQuantumCoinGameResult, quantumCoinGame } from 'library';
82

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

0 commit comments

Comments
 (0)