From 6ee85640cfab75c284ed6db4d0e88526ba366438 Mon Sep 17 00:00:00 2001 From: Emily Stamm Date: Mon, 31 Dec 2018 13:50:45 -0600 Subject: [PATCH 1/2] created correct quantum die program --- examples/quantum_die.py | 122 +++++++++++++++------------------------- 1 file changed, 44 insertions(+), 78 deletions(-) diff --git a/examples/quantum_die.py b/examples/quantum_die.py index c2f3b8a0a..20aaf1339 100755 --- a/examples/quantum_die.py +++ b/examples/quantum_die.py @@ -1,79 +1,45 @@ -#!/usr/bin/env python - -############################################################################## -# Copyright 2016-2017 Rigetti Computing -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -############################################################################## - -import math -from pyquil import Program, get_qc -from pyquil.gates import H -from six.moves import range - - -def qubits_needed(number_of_sides): - """ - The number of qubits needed for a die of n faces. - """ - return int(math.ceil(math.log(number_of_sides, 2))) - - -def get_qvm(number_of_sides): - """ - Get a QVM to simulate the requested number of sides. - """ - return get_qc(f"{qubits_needed(number_of_sides)}q-qvm") - - -def die_program(number_of_sides): - """ - Generate a quantum program to roll a die of n faces. - """ - prog = Program() - n_qubits = qubits_needed(number_of_sides) - ro = prog.declare('ro', 'BIT', n_qubits) - # Hadamard initialize. - for q in range(n_qubits): - prog.inst(H(q)) - # Measure everything. - for q in range(n_qubits): - prog.measure(q, ro[q]) - return prog - - -def process_results(results): - """ - Convert n digit binary result from the QVM to a value on a die. - """ - raw_results = results[0] - processing_result = 0 - for each_qubit_measurement in raw_results: - processing_result = 2*processing_result + each_qubit_measurement - # Convert from 0 indexed to 1 indexed - die_value = processing_result + 1 - return die_value - - -def roll_die(qvm, number_of_sides): - """ - Roll an n-sided quantum die. - """ - die_compiled = qvm.compile(die_program(number_of_sides)) - return process_results(qvm.run(die_compiled)) - - -if __name__ == '__main__': +# ---------------------------- +# Quantum Die +# ---------------------------- +# Emily Stamm +# Dec 30, 2018 + +from pyquil import * +import numpy as np +from pyquil.quil import * + +def throw_polyhedral_die_helper(p,qc, num_qubits): + result_dict = qc.run_and_measure(p, trials = 1) + result = 1 + for i in range(num_qubits): + result += (2**i)*result_dict[i][0] + return result + +# throw_polyhedral_die(num_sides) +# ---------------------------- +# return the result of throwing a num_sides sided die by running a quantum program + +def throw_polyhedral_die(num_sides): + # Number of qubits needed for computation + num_qubits = int(np.ceil(np.log2(num_sides))) + # Initialize program + p = Program() + p.inst(H(i) for i in range(num_qubits)) + + + qc_string = str(num_qubits) + 'q-qvm' + qc = get_qc(qc_string) + + result = num_sides + 1 + # Throw dice until get a number <= number of sides of dice + # Note- will get anywhere from 1 to ceiling of log_2(num_sides) + # >= log_2(num_sides) + while result > num_sides: + result = throw_polyhedral_die_helper(p,qc,num_qubits) + return result + +def input_for_die(): number_of_sides = int(input("Please enter number of sides: ")) - qvm = get_qvm(number_of_sides) - print(f"The result is: {roll_die(qvm, number_of_sides)}") + print("The result is: ", throw_polyhedral_die(number_of_sides)) + +input_for_die() \ No newline at end of file From ff26d54b58cb32b7ddd4e152bbe5ee7a2a6bee06 Mon Sep 17 00:00:00 2001 From: Emily Stamm Date: Wed, 23 Jan 2019 20:42:59 -0600 Subject: [PATCH 2/2] fixed quantum die example so that roll is value <= number of sides --- examples/quantum_die.py | 124 ++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 44 deletions(-) diff --git a/examples/quantum_die.py b/examples/quantum_die.py index 20aaf1339..ab892481e 100755 --- a/examples/quantum_die.py +++ b/examples/quantum_die.py @@ -1,45 +1,81 @@ -# ---------------------------- -# Quantum Die -# ---------------------------- -# Emily Stamm -# Dec 30, 2018 - -from pyquil import * -import numpy as np -from pyquil.quil import * - -def throw_polyhedral_die_helper(p,qc, num_qubits): - result_dict = qc.run_and_measure(p, trials = 1) - result = 1 - for i in range(num_qubits): - result += (2**i)*result_dict[i][0] - return result - -# throw_polyhedral_die(num_sides) -# ---------------------------- -# return the result of throwing a num_sides sided die by running a quantum program - -def throw_polyhedral_die(num_sides): - # Number of qubits needed for computation - num_qubits = int(np.ceil(np.log2(num_sides))) - # Initialize program - p = Program() - p.inst(H(i) for i in range(num_qubits)) - - - qc_string = str(num_qubits) + 'q-qvm' - qc = get_qc(qc_string) - - result = num_sides + 1 - # Throw dice until get a number <= number of sides of dice - # Note- will get anywhere from 1 to ceiling of log_2(num_sides) - # >= log_2(num_sides) - while result > num_sides: - result = throw_polyhedral_die_helper(p,qc,num_qubits) - return result - -def input_for_die(): - number_of_sides = int(input("Please enter number of sides: ")) - print("The result is: ", throw_polyhedral_die(number_of_sides)) +#!/usr/bin/env python + +############################################################################## +# Copyright 2016-2017 Rigetti Computing +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +############################################################################## + +import math +from pyquil import Program, get_qc +from pyquil.gates import H +from six.moves import range + + +def qubits_needed(number_of_sides): + """ + The number of qubits needed for a die of n faces. + """ + return int(math.ceil(math.log(number_of_sides, 2))) + + +def get_qvm(number_of_sides): + """ + Get a QVM to simulate the requested number of sides. + """ + return get_qc(f"{qubits_needed(number_of_sides)}q-qvm") + -input_for_die() \ No newline at end of file +def die_program(number_of_sides): + """ + Generate a quantum program to roll a die of n faces. + """ + prog = Program() + n_qubits = qubits_needed(number_of_sides) + ro = prog.declare('ro', 'BIT', n_qubits) + # Hadamard initialize. + for q in range(n_qubits): + prog.inst(H(q)) + # Measure everything. + for q in range(n_qubits): + prog.measure(q, ro[q]) + return prog + + +def process_results(results): + """ + Convert n digit binary result from the QVM to a value on a die. + """ + raw_results = results[0] + processing_result = 0 + for each_qubit_measurement in raw_results: + processing_result = 2*processing_result + each_qubit_measurement + # Convert from 0 indexed to 1 indexed + die_value = processing_result + 1 + return die_value + + +def roll_die(qvm, number_of_sides): + """ + Roll an n-sided quantum die. + """ + die_compiled = qvm.compile(die_program(number_of_sides)) + return process_results(qvm.run(die_compiled)) + + +if __name__ == '__main__': + number_of_sides = int(input("Please enter number of sides: ")) + qvm = get_qvm(number_of_sides) + die_value = roll_die(qvm, number_of_sides) + while die_value > number_of_sides: die_value = roll_die(qvm, number_of_sides) + print(f"The result is: {die_value}") \ No newline at end of file