Skip to content

Commit

Permalink
v0.4 add AllInit
Browse files Browse the repository at this point in the history
  • Loading branch information
dedztbh committed Sep 22, 2020
1 parent 7e4f99a commit cd4fd27
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Tester: Run a simulation on circuit with initial state |00...0>, then print the

TFinder: Similar to Tester, but only print the circuit's matrix.

AllInit: Similar to Tester, but run for each possible initial state (2^N of them).

## Available Commands

- Not i
Expand All @@ -33,3 +35,4 @@ TFinder: Similar to Tester, but only print the circuit's matrix.
- SqrtNot i
- SqrtNotDag i
- SqrtSwap i j (Not implemented yet)
- Measure i (Measure i time on standard basis, not implemented yet)
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.3"
version = "0.4"

val ejmlVersion = "0.39"

Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/operator/AllInit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package operator

import util.*
import kotlin.math.pow

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

fun allStates(n: Int) =
Array(2.0.pow(n).toInt()) {
IntArray(n) { i -> (it shr i) and 1 }.apply { reverse() }
}


class AllInit(N: Int) : TFinder(N) {
override fun printResult() {
super.printResult()
println("\nFinal states: ")
allStates(N).forEach { arr ->
println("Init |${arr.joinToString("")}>")
var jointState = I1
arr.forEach { i ->
jointState = (if (i == 0) KET0 else KET1) kron jointState
}
(opMatrix * jointState).print()
println()
}
}
}
3 changes: 0 additions & 3 deletions src/main/kotlin/operator/TFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ open class TFinder(val N: Int) : Operator {
val jointStateSize = 2.0.pow(N).toInt()
val IN2 = Ops.identity(jointStateSize)

/** 2^N by 1 column vector */
val jointState =
Matrix(jointStateSize, 1).apply { set(0, 0, 1.0, 0.0) }
var opMatrix = IN2

val IKronTable = Array(N + 1) { I1 }.also {
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/operator/Tester.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package operator

import util.Matrix
import util.times

/**
* Created by DEDZTBH on 2020/09/22.
* Project KuantumCircuitSim
*/
class Tester(N: Int) : TFinder(N) {
/** 2^N by 1 column vector */
val jointState =
Matrix(jointStateSize, 1).apply { set(0, 0, 1.0, 0.0) }

override fun printResult() {
super.printResult()
println("\nFinal state: ")
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/util/matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ val H = Matrix(
val I1 = Ops.identity(1)
val I2 = Ops.identity(2)

val KET0 = Matrix(arrayOf(doubleArrayOf(1.0, 0.0), doubleArrayOf(0.0, 0.0)))
val KET1 = Matrix(arrayOf(doubleArrayOf(0.0, 0.0), doubleArrayOf(1.0, 0.0)))

val KETBRA0 = Ops.diag(1.0, 0.0, 0.0, 0.0)
val KETBRA1 = Ops.diag(0.0, 0.0, 1.0, 0.0)
val SQRT_NOT = Matrix(
Expand Down

0 comments on commit cd4fd27

Please sign in to comment.