-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from EarlOld/quantum-coin-game
Quantum Coin Game
- Loading branch information
Showing
9 changed files
with
94 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |