oreilly-qc / oreilly-qc.github.io

Code samples for Programming Quantum Computers, from O'Reilly Media
130 stars 59 forks source link

Dependency on Jupitor to be documented #4

Closed ermadhan closed 4 years ago

ermadhan commented 4 years ago

Can you please update the documentation to use Jupyter for running the qiskit samples? Command line IPython as well as Python CLI will not execute the samples correctly.

machinelevel commented 4 years ago

Hi @ermadhan, thanks for this! I had thought the notebooks would be easier for most folks, but I should be able to modify the samples so they support CLI as well. Will take a pass at that asap.

machinelevel commented 4 years ago

Okay, that's done. I haven't tested them all yet, but it was an easy change. The Qiskit samples added so far should now support CLI as well as notebook, and I'll use this form when porting the new ones. :] Thanks so much for your feedback. If you have more issues or suggestions, please lmk.

machinelevel commented 4 years ago

Example of running from CLI:

python ./ch02_02_random_byte.py
counts: {'11111100': 1}
Random number: 63
machinelevel commented 4 years ago

(also, please feel free to re-open this if you find it's not fixed)

ermadhan commented 4 years ago

Thank you. I was facing issue with %matplotlib and your fix is fine. Is there plan to line examples using Qutip? I am learning through your book and will help me to compare using different python libraries.

machinelevel commented 4 years ago

You're very welcome! I hope this is useful, and I hope the book helps.

As for other engines, absolutely! Q# (Microsoft QDK) versions of the samples are being added very soon with the help of some friends at MS, and I'd love to add Qutip versions of all of the samples as well. It takes time, but if someone involved in Qutip wants to contribute, I'd welcome that. :]

Also, I've just added a script to run all of the Qiskit samples from CLI, here's the output:

python ./run_all_samples.py

ch02_01_random_bit.py--------------------------------------------
counts: {'0': 1}
[1.+0.j 0.+0.j]
ch02_02_random_byte.py--------------------------------------------
counts: {'10100110': 1}
Random number: 101
ch02_03_root_not.py--------------------------------------------
[0.+0.j 1.+0.j]
ch02_04_spy_hunter.py--------------------------------------------
counts: {'0 0 0 1 0': 1}
No spies detected.
[1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
ch03_01_seperable_qubits.py--------------------------------------------
[0.5+0.j 0. +0.j 0.5+0.j 0. +0.j 0.5+0.j 0. +0.j 0.5+0.j 0. +0.j]
ch03_02_entangled_qubits.py--------------------------------------------
counts: {'0 0': 1}
[1.+0.j 0.+0.j 0.+0.j 0.+0.j]
ch03_03_phase_kickback.py--------------------------------------------
[0.5+0.j 0.5+0.j 0.5+0.j 0.5+0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
ch03_04_swaptest.py--------------------------------------------
counts: {'1': 1}
[0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j]
ch03_05_custom_cphase.py--------------------------------------------
[ 0.5  +0.j    -0.354-0.354j  0.5  +0.j     0.354+0.354j]
ch03_06_remote_random.py--------------------------------------------
counts: {'0 0': 1}
[0.924+0.383j 0.   +0.j    0.   +0.j    0.   +0.j   ]
ch04_01_basic_teleportation.py--------------------------------------------
counts: {'0 0 0': 1}
[1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j]
ch04_02_teleport_fly.py--------------------------------------------
Finished in 7.25794792175293 seconds
counts: {'000000 000000': 1}
[ 0.078-0.j  0.078+0.j -0.172+0.j ...  0.   +0.j  0.   +0.j  0.   +0.j]
ch05_01_increment.py--------------------------------------------
|1> (0.707+0j)
|5> (0.5+0.5j)
ch05_02_quantum_add.py--------------------------------------------
|18> (0.5+0j)
|22> (0.354+0.354j)
|52> 0.5j
|56> (-0.354+0.354j)
ch05_03_add_squared.py--------------------------------------------
|18> (0.5-0j)
|22> (0.354+0.354j)
|58> 0.5j
|62> (-0.354+0.354j)
ch05_04_conditional_execution.py--------------------------------------------
|13> (0.5+0j)
|17> (0.5+0j)
|29> 0.5j
|33> 0.5j
ch05_05_conditional_phase.py--------------------------------------------
|8> (-0.354+0j)
|9> (-0.354+0j)
|10> (-0.354+0j)
|11> (0.354+0j)
|24> (0.354+0j)
|25> (0.354+0j)
|26> (0.354+0j)
|27> (0.354+0j)
ch05_06_cnot_logic.py--------------------------------------------
counts: {'111 11 1': 1}
|7> (1+0j)
ch06_01_grover_iterations.py--------------------------------------------
|0> (-0.167+0j) probability = 2.7889%
|1> (-0.167+0j) probability = 2.7889%
|2> (-0.167+0j) probability = 2.7889%
|3> (0.763-0j) probability = 58.2169%
|4> (-0.167+0j) probability = 2.7889%
|5> (-0.167+0j) probability = 2.7889%
|6> (-0.167+0j) probability = 2.7889%
|7> (-0.167+0j) probability = 2.7889%
|8> (-0.167+0j) probability = 2.7889%
|9> (-0.167+0j) probability = 2.7889%
|10> (-0.167+0j) probability = 2.7889%
|11> (-0.167+0j) probability = 2.7889%
|12> (-0.167+0j) probability = 2.7889%
|13> (-0.167+0j) probability = 2.7889%
|14> (-0.167+0j) probability = 2.7889%
|15> (-0.167+0j) probability = 2.7889%
Total probability: 100%
ch06_02_grover_freq.py--------------------------------------------
|0> (-0.167+0j) probability = 2.7889%
|1> (-0.167+0j) probability = 2.7889%
|2> (-0.167+0j) probability = 2.7889%
|3> (0.763-0j) probability = 58.2169%
|4> (-0.167+0j) probability = 2.7889%
|5> (-0.167+0j) probability = 2.7889%
|6> (-0.167+0j) probability = 2.7889%
|7> (-0.167+0j) probability = 2.7889%
|8> (-0.167+0j) probability = 2.7889%
|9> (-0.167+0j) probability = 2.7889%
|10> (-0.167+0j) probability = 2.7889%
|11> (-0.167+0j) probability = 2.7889%
|12> (-0.167+0j) probability = 2.7889%
|13> (-0.167+0j) probability = 2.7889%
|14> (-0.167+0j) probability = 2.7889%
|15> (-0.167+0j) probability = 2.7889%
Total probability: 100%
ch06_03_grover_multi.py--------------------------------------------
|0> (0.564-0j) probability = 31.8096%
|1> (0.564-0j) probability = 31.8096%
|2> (0.564-0j) probability = 31.8096%
|3> (-0.059+0j) probability = 0.3481%
|4> (-0.059+0j) probability = 0.3481%
|5> (-0.059+0j) probability = 0.3481%
|6> (-0.059+0j) probability = 0.3481%
|7> (-0.059+0j) probability = 0.3481%
|8> (-0.059+0j) probability = 0.3481%
|9> (-0.059+0j) probability = 0.3481%
|10> (-0.059+0j) probability = 0.3481%
|11> (-0.059+0j) probability = 0.3481%
|12> (-0.059+0j) probability = 0.3481%
|13> (-0.059+0j) probability = 0.3481%
|14> (-0.059+0j) probability = 0.3481%
|15> (-0.059+0j) probability = 0.3481%
Total probability: 100%
ch07_01_qft_distinguish.py--------------------------------------------
|8> (1+0j) probability = 100.0%
Finished 22 samples in 66 seconds.