Skip to content

Commit

Permalink
v0.5 add measure
Browse files Browse the repository at this point in the history
  • Loading branch information
dedztbh committed Sep 22, 2020
1 parent cd4fd27 commit e528050
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ AllInit: Similar to Tester, but run for each possible initial state (2^N of them
- SqrtNot i
- SqrtNotDag i
- SqrtSwap i j (Not implemented yet)
- Measure i (Measure i time on standard basis, not implemented yet)
- Measure n
+ Measures the joint qubit state n times using standard basis
+ Only works when using Tester
+ Will end command read
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id("com.github.johnrengelman.shadow") version "6.0.0"
}
group = "com.dedztbh"
version = "0.4"
version = "0.5"

val ejmlVersion = "0.39"

Expand Down
16 changes: 1 addition & 15 deletions example.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
Hadamard 2
CNot 1 2
TDag 2
CNot 0 2
T 2
CNot 1 2
TDag 2
CNot 0 2
TDag 1
T 2
CNot 0 1
Hadamard 2
TDag 1
CNot 0 1
T 0
S 1
Measure 10
5 changes: 4 additions & 1 deletion src/main/kotlin/operator/TFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ open class TFinder(val N: Int) : Operator {
// "SqrtSwap" -> {
// TODO: Implement SqrtSwap
// }
else -> return -1
else -> {
System.err.println("Unknown command \"$cmd\". Stop reading commands.")
return -1
}
}
opMatrix = newOp * opMatrix
return 0
Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/operator/Tester.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package operator

import org.ejml.data.Complex_F64
import util.Matrix
import util.readInt
import util.times
import util.upperBound
import kotlin.random.Random

/**
* Created by DEDZTBH on 2020/09/22.
Expand All @@ -12,6 +16,32 @@ class Tester(N: Int) : TFinder(N) {
val jointState =
Matrix(jointStateSize, 1).apply { set(0, 0, 1.0, 0.0) }

override fun runCmd(cmd: String): Int {
if (cmd == "Measure") {
val i = readInt()
val results = opMatrix * jointState
val probs = mutableListOf(0.0)
val labels = mutableListOf<Int>()
val a = Complex_F64()
for (j in 0 until results.numRows) {
results.get(0, j, a)
if (a.magnitude2 > 0) {
labels.add(j)
probs.add(a.magnitude2 + probs.last())
}
}
println("Measurement(s):")
repeat(i) {
val r = Random.nextDouble()
val index = probs.upperBound(r)
println("|${labels[index].toString(2).run { padStart(N - length + 1, '0') }}>")
}
println()
return 1
}
return super.runCmd(cmd)
}

override fun printResult() {
super.printResult()
println("\nFinal state: ")
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/util/algo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util

/**
* Created by DEDZTBH on 2020/09/22.
* Project KuantumCircuitSim
*/

fun <T : Comparable<T>> List<T>.upperBound(key: T): Int {
var low = 0
var high = size - 1
while (low < high) {
val mid = low + (high - low + 1) / 2
if (this[mid] <= key) {
low = mid
} else {
high = mid - 1
}
}
return low
}

0 comments on commit e528050

Please sign in to comment.