Git Product home page Git Product logo

quantumjava's Introduction

Quantum Computing samples in Java

This repository contains the source code for the samples discussed in Quantum Computing for Developers

Quantum Computing for Developers

It is based on the Strange Quantum Simulator, which provides an execution environment for quantum algorithms in Java.
Some samples use a UI, e.g. to visualize the quantum circuit. In that case, StrangeFX is used, which is a JavaFX-based framework that allows the visualisation of quantum circuits.

The samples in this repository correspond to the chapters in the book. Don't worry if you don't have the book, you can still run the samples.

Chapter 2: HelloWorld, Quantum Computing

HelloStrange

Chapter 3: Qubits and Quantum Gates, the basic units in Quantum Computing

Pauli X
Pauli X with user interface

Chapter 4: Superposition

Hadamard

Chapter 5: Entanglement

Classic coins
Quantum coins
CNot gate
Bell state

Chapter 6: Quantum networking, the basics

Classic network
No-cloning theorem
Quantum Teleportation
Quantum Repeater

Chapter 7: Our HelloWorld explained

RandomBit
RandomBit with debug
Quantum Adder
Quantum Adder with carry bit

Chapter 8: Secure Communication using quantum computing

A first (naive) approach
Applying 2 Hadamard gates
Using superposition
Guess the possibilities
QKD in Java

Chapter 9: Deutsch-Jozsa algorithm

Constant and balanced functions
Reversible gates
Quantum oracle
Applying a Quantum oracle
Deutsch algorithm
Deutsch Jozsa algorithm

Chapter 10: Grover's Search Algorithm

A classic search function
Quantum search
The algorithm behind Grover's search
The algorithm, step by step
The quantum Oracle in Grover's search

Chapter 11: Shor's Algorithm

A classic factoring approach
A classic implementation of a quantum factoringapproach
A quantum factoring approach

quantumjava's People

Contributors

dependabot[bot] avatar johanvos avatar kelumkps avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

quantumjava's Issues

ch1 - time - issue in strange version of pom.xml

Hello,

If I run "ch1 - time" example, I get an error stating that 0.1.2-SNAPSHOT version of org.redfx.strange cannot be found.
In corresponding pom.xml, I suggest to replace 0.1.2-SNAPSHOT by 0.1.3.

Regards

MacOS application has rendering issues with vertical ProbabilityGate and horizontal QubitFlows

An application doesn't render right with vertical probability gates. It seems to only render the first QuantamFlow. An example is ch07 randombitdebug from an application.

I made a change to ProbabilityGate where if you click on a cell you get slightly more information. There were issues with this because the ProbabilityGate runs vertically across all horizontal QubitFlows. Only cells in the first QubitFlow received the mouse clicks. To work around this I added the mouse listener to QubitBoard and it determines if a ProbabilityGate has been clicked on and which cell. It then shows a window with a little more information on that cell.

I was going to just mention this as a comment to Windows 10 application but noticed that it appears related to a MacOS application rendering circuits with ProbibilityGate(s) correctly. They also only seem to show the first QubitFlow. Mouse clicks on the invisible ProbabilityGate parts still work. Since this doesn't seem to only apply to my changes I thought it might merit a separate issue.

A MacOS version of the application is again available at http://mikehall.pairserver.com/qc.dmg. I have added signing. Although I think I need to renew my Apple dev account. The new window includes the superposition probability visualization I also mentioned in the above prior issue. I suggested that might be good for the wire ending visualization but will hold off on trying to change that to see if you have any thoughts.

Next I thought I'd add a File menu as previously mentioned. Maybe look at XML to circuit to XML with strange. Possibly look at the application rendering with a ProbabilityGate since I had just been looking at something sort of related. I noticed you have some drag and drop in place so maybe you had some thoughts along those lines?

quantumjava/ch11/jshell/checkperiod

Question: shouldn't it be "a" instead of "p" inside the loop for a^p?

for (int i = 0; i < p; i++) {
u = (u * a) % N;
}
vs
for (int i = 0; i < p; i++) {
u = (u * p) % N;
}

OS/X version updates - Book Chapters 7-11

ch07


pom.xml add1
javafx-controls to 19.0.2.1 from 13.0.1
strange version to 0.1.3 from 0.0.19
strangefx version to 0.1.4 from 0.0.10
rendered garbled text

pom.xml add2
javafx-controls to 19.0.2.1 from 13.0.1
strange version to 0.1.3 from 0.1.2-SNAPSHOT
strangefx version to 0.1.4 from 0.1.2-SNAPSHOT
snapshot not found

pom.xml randombitdebug
strangefx version to 0.1.4 from 0.0.10
get rid of nd4j dependency warnings

ch08


pom.xml naive
strangefx version to 0.1.4 from 0.0.10
get rid of nd4j dependency warnings

pom.xml haha, guess, superposition, bb84
javafx-controls to 19.0.2.1 from 13.0.1
strangefx version to 0.1.4 from 0.0.10
get rid of nd4j dependency warnings
rendered garbled text

ch09


pom.xml oracle
javafx-controls to 19.0.2.1 from 13.0.1
strangefx version to 0.1.4 from 0.0.10
get rid of nd4j dependency warnings
rendered garbled text

pom.xml applyoracle
strangefx version to 0.1.4 from 0.0.10
get rid of nd4j dependency warnings

Changed
Renderer.renderProgram(program, "deutsch");
to pass title at this point. So local repository strangefx 0.1.5
and strange 0.1.4 with changes were used from this point on.
Retroactive on prior as well.

pom.xml deutschjozsa
javafx-controls to 19.0.2.1 from 13.0.1

ch10


pom.xml grover, stepbystepgrover, groveroracle
javafx-controls to 19.0.2.1 from 13.0.1

ch11


pom.xml classicfactor
strangefx version to 0.1.4 from 0.0.10
get rid of nd4j dependency warnings

paulixui OS/X - *** Terminating app due to uncaught exception 'NSInternalInconsistencyException'

Actually seems to be a Homebrew issue but if anyone else runs into it.

For this example (paulixui)
mvn javafx:run

gets the indicated error.
This indicates it should of been fixed at 19.0.2
https://stackoverflow.com/questions/74630535/assertion-failure-when-trying-to-compile-javafx-project-using-maven

I am using a homebrew maven
mvn --version
Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9)
Maven home: /usr/local/Cellar/maven/3.9.4/libexec
Java version: 20.0.1, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk/20.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "13.4.1", arch: "x86_64", family: "mac"

Which apparently uses it's own homebrew (Cellar) jdk.

Setting to use the Oracle 19.0.2
/usr/libexec/java_home -v 19
/Library/Java/JavaVirtualMachines/jdk-19.0.2.jdk/Contents/Home

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-19.0.2.jdk/Contents/Home

and it ran correctly. So the homebrew jdk seems to have missed the fix.

OS/X javafx application fails to launch with strangefx

javafx application including the strangefx jar seems to hang on launch. It seems to be automatically trying to start a jdk internal thread - com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable
The application has done nothing related at this time that I am aware of.

"main" #1 [8963] prio=5 os_prio=31 cpu=139.74ms elapsed=24.69s tid=0x00007fd8ab00b000 nid=8963 waiting on condition [0x000070000a332000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@20/Native Method)
- parking to wait for <0x000000070fd2be68> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.park(java.base@20/Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(java.base@20/Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(java.base@20/Unknown Source)
at java.util.concurrent.CountDownLatch.await(java.base@20/Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(javafx.graphics@19/Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(javafx.graphics@19/Unknown Source)
at javafx.application.Application.launch(javafx.graphics@19/Unknown Source)
at us.hall.qcapp.QuantumJava.main(QuantumJava.java:77)
at java.lang.invoke.LambdaForm$DMH/0x0000000800080400.invokeStatic(java.base@20/LambdaForm$DMH)
at java.lang.invoke.LambdaForm$MH/0x0000000800101400.invoke(java.base@20/LambdaForm$MH)
at java.lang.invoke.Invokers$Holder.invokeExact_MT(java.base@20/Invokers$Holder)
at jdk.internal.reflect.DirectMethodHandleAccessor.invokeImpl(java.base@20/Unknown Source)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(java.base@20/Unknown Source)
at java.lang.reflect.Method.invoke(java.base@20/Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(javafx.graphics@19/Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(javafx.graphics@19/Unknown Source)
at java.lang.invoke.LambdaForm$DMH/0x0000000800002400.invokeStatic(java.base@20/LambdaForm$DMH)
at java.lang.invoke.LambdaForm$MH/0x0000000800003c00.invoke(java.base@20/LambdaForm$MH)
at java.lang.invoke.LambdaForm$MH/0x0000000800080000.invokeExact_MT(java.base@20/LambdaForm$MH)
at jdk.internal.reflect.DirectMethodHandleAccessor.invokeImpl(java.base@20/Unknown Source)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(java.base@20/Unknown Source)
at java.lang.reflect.Method.invoke(java.base@20/Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(java.base@20/Unknown Source)

"QuantumRenderer-0" #20 [29187] daemon prio=5 os_prio=31 cpu=50.61ms elapsed=24.57s tid=0x00007fd8ac813400 nid=29187 waiting on condition [0x000070000b56c000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@20/Native Method)
- parking to wait for <0x000000070fcb67f0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(java.base@20/Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionNode.block(java.base@20/Unknown Source)
at java.util.concurrent.ForkJoinPool.unmanagedBlock(java.base@20/Unknown Source)
at java.util.concurrent.ForkJoinPool.managedBlock(java.base@20/Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(java.base@20/Unknown Source)
at java.util.concurrent.LinkedBlockingQueue.take(java.base@20/Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(java.base@20/Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(java.base@20/Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(java.base@20/Unknown Source)
at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(javafx.graphics@19/Unknown Source)
at java.lang.Thread.runWith(java.base@20/Unknown Source)
at java.lang.Thread.run(java.base@20/Unknown Source)

ch5 - cnot example - wrong final qubit states in visual output?

When I run the cnot example I get the following visual output. It appears that the pink boxes showing the final qubit states have all been set to the values in the final plot generated from run11():

image
image

If run the four scenarios individually (eg by commenting out 3 of the 4 calls in main) I get the correct output. Here's what I get from run00() when I run it on its own:

image

Not sure if I've done something wrong but thought I'd report it in case it's a real problem

Windows 10 application using quantumjava, strange, strangefx gets graphics errors

I'm not sure if you're interested in the code running as an application but if so this has had an issue below.

I have tried this Windows 10 virtual box and on a old Mac laptop I copied Windows 10 over the top of, same error.
Google shows others have had the issue and I saw different claims that different modular parts would fix it, but have had no success. I even looked at what GitHub quantumjava maven does and believe I have tried the matching parameters.

This was more successful on OS/X. Again if of any interest and you have access to a Mac I have put most of the pertinent parts in a dmg at http://mikehall.pairserver.com/qc.dmg. Not signed at all so it may give some grief over that. I can sign the app if needed.
It had problems with maryqubit being started as a second application. So I set that to run as a separate process. That runs it but I have figured out how to get the process to exit cleanly. There are some rendering issues. Missing wires, ones with probabilty gates don't seem to appear right. Some of the other later ones look off. But they all run.

For changes I have considered. I had a little difficulty understanding the probability gate. Probably because I looked at the rendering before being clear on the text. I was thinking the cells would somehow correspond to the qubit's they were across from. Where actually each cell represents the states of all cells? I was thinking a popup on clicking one of the cells could give you more information.

For displaying the qubit states I was also thinking something like this...
https://stackoverflow.com/questions/68903791/javafx-circle-linear-gradient-fill-by-percentage
maybe better visualized my understanding of superposition. I was thinking of maybe trying to add a property that would allow this visualization to be selected.

An easy new feature would be a file menu that allowed to open, read, compile, run and render a user java program.
Sort of a start at an IDE?

Considerably more involved IDE like would be making this javafx scene builder like. Have an interface where you could visually drag qubits, gates, wires, oracles... and from that generate XML. From the XML the java quantum circuit boilerplate could be generated. With XML being language independent you could possibly generate boilerplate for other languages - python, c++, qasm. Since this is just for the quantum circuitry it might not be exceedingly complex. A thought.

my 2 cents anyhow.

The actual windows error issue follows. I may continue with some of this on OS/X but taking a break for now.

Graphics Device initialization failed for : d3d, sw
Error initializing QuantumRenderer: no suitable pipeline found
java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
at [email protected]/com.sun.javafx.tk.quantum.QuantumRenderer.getInstance(QuantumRenderer.java:283)
at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.init(QuantumToolkit.java:253)
at [email protected]/com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:263)
at [email protected]/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:290)
at [email protected]/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:162)
at [email protected]/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:651)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:409)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
at [email protected]/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:95)
at [email protected]/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
at java.base/java.lang.Thread.run(Thread.java:1623)
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: No toolkit found
at [email protected]/com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:275)
at [email protected]/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:290)
at [email protected]/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:162)
at [email protected]/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:651)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:409)
at [email protected]/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 2 more

Mary Had a Little Qubit - error in the screenshot

Hello,

I have bought the book and I was training on the exercice "Mary Had a Little Qubit" of chapter 5.
I think there is a mistake in the image https://github.com/johanvos/quantumjava/blob/master/ch05/maryqubit/docs/mary3gates.png
Indeed, if I read the pipes correctly, here is what happened :

  1. Initial state : all sheeps are in "off" state
  2. sheep0 goes through H gate. It goes in a superposition state. 50%
  3. sheep1 goes through C gate. It is the source of an etanglement state. On the image it is represented by the fact that the dot symbol is on the pipe of sheep1
  4. sheep0 goes through C gate as well. It becomes entangled with sheep1 and it becomes the "target" of sheep1. On the image it is represented by the fact that the "+" symbol is on the pipe of sheep0. As far as I understand, this means that sheep0 will have the state defined by sheep1. Sheep1 is in off state, then sheep0 should go to off state.

If I execute these steps with the simulator, I get this :

Ship0 : 50%
Ship1 : Off

If I take a look to the image, I get this :
Ship0 : 50%
Ship1 : 50%

If I trust my understanding, I get this :
Ship0 : Off
Ship1 : Off

Could you please check this case and clarify?

Thanks

OS/X version updates and minor rendering bug - Book Chapters 1-6

I ran into the following running the book examples on OS/X in Chapters 1-6.
In some cases 19.0.2.1 is used for the javafx-controls dependency. This is consistent with my work around for
#22
I think examples that did run at least sometimes used 16 which also worked. I don't know if javafx usually mirrors jdk versions or not but 19.0.2.1 worked for me with that jdk version. fi
The garbled text I remember being an OS/X javafx bug sometime ago, that was fixed.
I did some tracking down on the Cz rendering wrong.

quantumjava changes or differences:

pom.xml
strangefx version to 0.1.4 from 0.0.10
javafx-controls to 19.0.2.1 from 13.0.1

hadamard rendered garbled text
hadamard2 failed on nd4j dependencies

pom.xml maryqubit
javafx-controls to 19.0.2.1 from 13.0.1
rendered garbled text

pom.xml teleport
strangefx version to 0.1.4 from 0.1.2-SNAPSHOT
snapshot not found
rendered Bob q[2]|0> gate is Z, figure 6.22 (and code) is Cz
GateSymbol: createCZNode sets wrong Label (line 368)

pom.xml repeater
javafx-controls to 19.0.2.1 from 13.0.1
strangefx version to 0.1.4 from 0.0.10
rendered garbled text

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.