Last active
September 8, 2023 17:52
-
-
Save primaryobjects/4082bd04aa7cb2b8f5457f32757777d4 to your computer and use it in GitHub Desktop.
Rock paper scissors quantum computing qiskit using 1 qubit
This file contains hidden or 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
| # Import Qiskit and numpy | |
| from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer | |
| import numpy as np | |
| # Define the quantum register and the classical register | |
| qreg = QuantumRegister(1) | |
| creg = ClassicalRegister(1) | |
| qc = QuantumCircuit(qreg, creg) | |
| # Define the basis states for rock, paper, and scissors | |
| rock = [1, 0] | |
| paper = [0, 1] | |
| scissors = [1/np.sqrt(2), 1/np.sqrt(2)] | |
| # Define the dictionary for the moves and their outcomes | |
| moves = { | |
| 'rock': { | |
| 'state': rock, | |
| 'beats': 'scissors', | |
| 'losesTo': 'paper' | |
| }, | |
| 'paper': { | |
| 'state': paper, | |
| 'beats': 'rock', | |
| 'losesTo': 'scissors' | |
| }, | |
| 'scissors': { | |
| 'state': scissors, | |
| 'beats': 'paper', | |
| 'losesTo': 'rock' | |
| } | |
| } | |
| # Ask the user to enter their choice | |
| user_choice = input("Enter a choice (rock, paper, scissors): ").lower() | |
| # Validate the user input | |
| while user_choice not in moves.keys(): | |
| user_choice = input("Invalid choice. Must be rock, paper, or scissors: ").lower() | |
| # Print the user choice | |
| print(f"You chose {user_choice}.") | |
| # Initialize the qubit with the user choice state | |
| qc.initialize(moves[user_choice]['state'], 0) | |
| # Apply a Hadamard gate to the qubit to create a superposition of 0 and 1 | |
| qc.h(0) | |
| # Measure the qubit | |
| qc.measure(qreg, creg) | |
| # Print the circuit | |
| print(qc) | |
| # Execute the circuit on the Qasm simulator | |
| simulator = Aer.get_backend('qasm_simulator') | |
| result = execute(qc, simulator, shots=1).result() | |
| # Get the counts of the outcomes | |
| counts = result.get_counts(qc) | |
| # Get the binary string of the outcome | |
| outcome = list(counts.keys())[0] | |
| # Get the first bit of the outcome binary string | |
| outcome_bit = outcome[0] | |
| # Get the computer choice from the outcome bit value | |
| computer_choice = list(moves.keys())[int(outcome_bit)] | |
| # Print the computer choice | |
| print(f"The computer chose {computer_choice}.") | |
| # Compare the user choice and the computer choice to determine the winner | |
| if user_choice == computer_choice: | |
| print("It's a tie.") | |
| elif moves[user_choice]['beats'] == computer_choice: | |
| print("You win!") | |
| else: | |
| print("You lose.") |
This file contains hidden or 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
| from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer | |
| import numpy as np | |
| # Define the quantum register and the classical register | |
| qreg = QuantumRegister(2) | |
| creg = ClassicalRegister(2) | |
| # Define the basis states for rock, paper, and scissors | |
| rock = [1, 0, 0, 0] | |
| paper = [0, 1, 0, 0] | |
| scissors = [1/np.sqrt(2), 0, 1/np.sqrt(2), 0] | |
| # Define the dictionary for the moves and their outcomes | |
| moves = { | |
| 'rock': { | |
| 'state': rock, | |
| 'beats': 'scissors', | |
| 'losesTo': 'paper', | |
| 'shortcut': 'r' | |
| }, | |
| 'paper': { | |
| 'state': paper, | |
| 'beats': 'rock', | |
| 'losesTo': 'scissors', | |
| 'shortcut': 'p' | |
| }, | |
| 'scissors': { | |
| 'state': scissors, | |
| 'beats': 'paper', | |
| 'losesTo': 'rock', | |
| 'shortcut': 's' | |
| }, | |
| 'quit': { | |
| 'shortcut': 'q' | |
| } | |
| } | |
| user_choice = '' | |
| while user_choice != 'quit': | |
| # Ask the user to enter their choice | |
| user_choice = input("Enter a choice (rock, paper, scissors, quit): ").lower() | |
| # Check if the user entered a shortcut and convert it to the corresponding move | |
| for move in moves: | |
| if user_choice == moves[move]['shortcut']: | |
| user_choice = move | |
| break | |
| # Validate the user input | |
| while user_choice not in moves.keys(): | |
| user_choice = input("Invalid choice. Must be rock, paper, or scissors: ").lower() | |
| # Check if the user entered a shortcut and convert it to the corresponding move | |
| for move in moves: | |
| if user_choice == moves[move]['shortcut']: | |
| user_choice = move | |
| break | |
| if user_choice == 'quit': | |
| break | |
| # Print the user choice | |
| print(f"You chose {user_choice}.") | |
| qc = QuantumCircuit(qreg, creg) | |
| # Initialize the qubits with the user choice state | |
| qc.initialize(moves[user_choice]['state'], [0, 1]) | |
| # Apply a Hadamard gate to the first qubit to create a superposition of 0 and 1 | |
| qc.h(0) | |
| # Measure the qubits | |
| qc.measure(qreg, creg) | |
| # Print the circuit | |
| print(qc) | |
| # Execute the circuit on the Qasm simulator | |
| simulator = Aer.get_backend('qasm_simulator') | |
| result = execute(qc, simulator, shots=1).result() | |
| # Get the counts of the outcomes | |
| counts = result.get_counts(qc) | |
| # Get the binary string of the outcome | |
| outcome = list(counts.keys())[0] | |
| print(outcome) | |
| # Get the right-most bit of the outcome binary string | |
| outcome_bit = outcome | |
| # Handle invalid move. | |
| if outcome_bit == '11': | |
| outcome_bit = '10' | |
| # Get the computer choice from the outcome bit value | |
| computer_choice = list(moves.keys())[int(outcome_bit, 2)] | |
| # Print the computer choice | |
| print(f"The computer chose {computer_choice}.") | |
| # Compare the user choice and the computer choice to determine the winner | |
| if user_choice == computer_choice: | |
| print("It's a tie.") | |
| elif moves[user_choice]['beats'] == computer_choice: | |
| print("You win!") | |
| else: | |
| print("You lose.") |
This file contains hidden or 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
| # Import Qiskit and numpy | |
| from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer | |
| import numpy as np | |
| # Define the quantum registers and the classical registers | |
| qreg = QuantumRegister(2) | |
| creg = ClassicalRegister(2) | |
| qc = QuantumCircuit(qreg, creg) | |
| # Define the basis states for rock, paper, and scissors | |
| rock = [1, 0] | |
| paper = [0, 1] | |
| scissors = [1/np.sqrt(2), 1/np.sqrt(2)] | |
| # Define the dictionary for the moves and their outcomes | |
| moves = { | |
| 'rock': { | |
| 'state': rock, | |
| 'beats': 'scissors', | |
| 'losesTo': 'paper' | |
| }, | |
| 'paper': { | |
| 'state': paper, | |
| 'beats': 'rock', | |
| 'losesTo': 'scissors' | |
| }, | |
| 'scissors': { | |
| 'state': scissors, | |
| 'beats': 'paper', | |
| 'losesTo': 'rock' | |
| } | |
| } | |
| # Ask the user to enter their choice | |
| user_choice = input("Enter a choice (rock, paper, scissors): ").lower() | |
| # Validate the user input | |
| while user_choice not in moves.keys(): | |
| user_choice = input("Invalid choice. Must be rock, paper, or scissors: ").lower() | |
| # Print the user choice | |
| print(f"You chose {user_choice}.") | |
| # Initialize the first qubit with the user choice state | |
| qc.initialize(moves[user_choice]['state'], 0) | |
| # Apply a Hadamard gate to the second qubit to create a superposition of 0 and 1 | |
| qc.h(1) | |
| # Apply a CNOT gate to both qubits to entangle them | |
| qc.cx(0, 1) | |
| # Measure the qubits | |
| qc.measure(qreg, creg) | |
| # Execute the circuit on the Qasm simulator | |
| simulator = Aer.get_backend('qasm_simulator') | |
| result = execute(qc, simulator, shots=1).result() | |
| # Get the counts of the outcomes | |
| counts = result.get_counts(qc) | |
| # Get the binary string of the outcome | |
| outcome = list(counts.keys())[0] | |
| # Get the decimal value of the outcome | |
| outcome_decimal = int(outcome, 2) | |
| # Get the computer choice from the outcome decimal value | |
| computer_choice = list(moves.keys())[outcome_decimal] | |
| # Print the computer choice | |
| print(f"The computer chose {computer_choice}.") | |
| # Compare the user choice and the computer choice to determine the winner | |
| if user_choice == computer_choice: | |
| print("It's a tie.") | |
| elif moves[user_choice]['beats'] == computer_choice: | |
| print("You win!") | |
| else: | |
| print("You lose.") |
This file contains hidden or 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
| Enter a choice (rock, paper, scissors, quit): s | |
| You chose scissors. | |
| ┌──────────────────────────────────┐┌───┐┌─┐ | |
| q0_0: ┤0 ├┤ H ├┤M├ | |
| │ Initialize(0.70711,0,0.70711,0) │└┬─┬┘└╥┘ | |
| q0_1: ┤1 ├─┤M├──╫─ | |
| └──────────────────────────────────┘ └╥┘ ║ | |
| c0: 2/══════════════════════════════════════╩═══╩═ | |
| 1 0 | |
| 00 | |
| The computer chose rock. | |
| You lose. | |
| Enter a choice (rock, paper, scissors, quit): s | |
| You chose scissors. | |
| ┌──────────────────────────────────┐┌───┐┌─┐ | |
| q0_0: ┤0 ├┤ H ├┤M├ | |
| │ Initialize(0.70711,0,0.70711,0) │└┬─┬┘└╥┘ | |
| q0_1: ┤1 ├─┤M├──╫─ | |
| └──────────────────────────────────┘ └╥┘ ║ | |
| c0: 2/══════════════════════════════════════╩═══╩═ | |
| 1 0 | |
| 10 | |
| The computer chose scissors. | |
| It's a tie. | |
| Enter a choice (rock, paper, scissors, quit): q |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment