-
Notifications
You must be signed in to change notification settings - Fork 342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
quantum die program #749
quantum die program #749
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for your contributions! I've written some comments. Many of them are stylistic, but important for consistency with the rest of the project. I highly recommend studying the source of the original file to see the style of it.
Lastly, how might you change the program so a user could run it on a real QPU as well?
examples/quantum_die.py
Outdated
@@ -1,79 +1,45 @@ | |||
#!/usr/bin/env python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not delete the header of this file, including the license and copyright notice.
Do note that your contributions are recorded as a part of the commit history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also run pep8
on this file and make the necessary fixes to make it PEP8 conforming.
examples/quantum_die.py
Outdated
# ---------------------------- | ||
# Emily Stamm | ||
# Dec 30, 2018 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please configure your editor to use 4 spaces, and not tabs, for indentation.
examples/quantum_die.py
Outdated
result += (2**i)*result_dict[i][0] | ||
return result | ||
|
||
# throw_polyhedral_die(num_sides) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation strings should be written as a part of the Python docstring in """
triple quotes. Take a gander at other parts of the pyQuil source code, including the previous code in this file, to see how it works.
examples/quantum_die.py
Outdated
|
||
def throw_polyhedral_die(num_sides): | ||
# Number of qubits needed for computation | ||
num_qubits = int(np.ceil(np.log2(num_sides))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the qubits_needed
function which you've deleted for this.
examples/quantum_die.py
Outdated
# Initialize program | ||
p = Program() | ||
p.inst(H(i) for i in range(num_qubits)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete unnecessary newlines
examples/quantum_die.py
Outdated
qc_string = str(num_qubits) + 'q-qvm' | ||
qc = get_qc(qc_string) | ||
|
||
result = num_sides + 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would replace
result = ...
while f(result):
result = ...
return result
with something more like
while True:
if not f(result):
return result
It's much simpler and doesn't require temporaries with broad scope.
examples/quantum_die.py
Outdated
import numpy as np | ||
from pyquil.quil import * | ||
|
||
def throw_polyhedral_die_helper(p,qc, num_qubits): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could choose a better name for this function. Also be sure to document it.
examples/quantum_die.py
Outdated
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be simpler to sum( ... )
over a comprehension here.
examples/quantum_die.py
Outdated
p.inst(H(i) for i in range(num_qubits)) | ||
|
||
|
||
qc_string = str(num_qubits) + 'q-qvm' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if the logic of handling the (pseudo-)random numbers was separate from the logic of constructing the qvm, which was separate from the logic of rejection sampling. Just my 2 cents though.
examples/quantum_die.py
Outdated
print(f"The result is: {roll_die(qvm, number_of_sides)}") | ||
print("The result is: ", throw_polyhedral_die(number_of_sides)) | ||
|
||
input_for_die() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the __main__
convention here.
Hi Robert,
Thank you for the comments - they were very helpful! I'm new to GitHub and
quantum computing, so I appreciate the feedback. I realize now I should
have made changes to the code already on there instead of just submitting
new code, but I figured you would want to make the changes. Would you like
me to make another pull request?
I've been mostly reading documentation or books on quantum computer theory
- are there any other resources you suggest?
Thank you again,
Emily
…On Wed, Jan 2, 2019 at 3:11 PM Robert Smith ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Thank you so much for your contributions! I've written some comments. Many
of them are stylistic, but important for consistency with the rest of the
project. I highly recommend studying the source of the original file to see
the style of it.
Lastly, how might you change the program so a user could run it on a real
QPU as well?
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> @@ -1,79 +1,45 @@
-#!/usr/bin/env python
Please do not delete the header of this file, including the license and
copyright notice.
Do note that your contributions are recorded as a part of the commit
history.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> -
-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
+
Please configure your editor to use 4 spaces, and not tabs, for
indentation.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> @@ -1,79 +1,45 @@
-#!/usr/bin/env python
Please also run pep8 on this file and make the necessary fixes to make it
PEP8 conforming.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> +# ----------------------------
+# 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)
Documentation strings should be written as a part of the Python docstring
in """ triple quotes. Take a gander at other parts of the pyQuil source
code, including the previous code in this file, to see how it works.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> +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)))
You could use the qubits_needed function which you've deleted for this.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> + 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))
+
delete unnecessary newlines
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> + 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'
I myself prefer a % or string interpolation, as opposed to manual
concatenation
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> +# 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
I would replace
result = ...
while f(result):
result = ...
return result
with something more like
while True:
if not f(result):
return result
It's much simpler and doesn't require temporaries with broad scope.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> - 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):
I think we could choose a better name for this function. Also be sure to
document it.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> -
-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):
it might be simpler to sum( ... ) over a comprehension here.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> + 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'
It would be nice if the logic of handling the (pseudo-)random numbers was
separate from the logic of constructing the qvm, which was separate from
the logic of rejection sampling. Just my 2 cents though.
------------------------------
In examples/quantum_die.py
<#749 (comment)>:
> 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()
Please use the __main__ convention here.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#749 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AZidvvpl_M0qytFdH8MyO3dZC7SwzC8eks5u_SCagaJpZM4ZloWr>
.
|
Hi @estamm12! The pull request process is something like this:
You're currently at step 4. Make the changes @tarballs-are-good suggested, commit and push them to your pyquil fork, and then we come back to review and merging. The standard textbook is Quantum Computation and Information by Nielsen and Chuang. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adapting your code into the existing example! This current diff is beautifully short
The current quantum die program in examples only rolls a die with the number of sides equal to a power of 2. So if for example the user inputs '5' for a 5-sided die, the result can be any integer from 1,...8. This program re-rolls the die if the result is greater than the number of sides.