Skip to content

Commit

Permalink
v1.5.4
Browse files Browse the repository at this point in the history
- added U/CU as alias for R/CR
- fixed matrix size mismatch when using CU
- updated example
  • Loading branch information
dedztbh committed Nov 14, 2020
1 parent 0df9534 commit 7d4503d
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 28 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ example: ```java -jar KuantumCircuitSim.jar example.txt Tester 3 -o output.csv -

Where example.txt contains list of commands and circuit matrix (csv) will be stored to output.csv and initial state is read from init.csv

Some examples for input command file, initial state (-i), and initial matrix (-m) are located in the example folder.

## Operators

TFinder: Generate the circuit's matrix and print it.
Expand Down Expand Up @@ -103,8 +105,9 @@ Commands are case-insensitive.
- SqrtNotDag i
- Rot i angle
+ Rotate qubit counterclockwise by angle (in radian)
- R i phi
- U i phi
+ Phase shift by phi (mapping |1> to e^(i*phi)|1>)
+ You can also use "R" instead of "U"

##### Parallel Mode
You can enclose the above single-qubit gates that can run in parallel between "ParStart" and "ParEnd" commands. This can speed up the simulation a lot. Make sure you are using only above single-qubit gates and no two gates are acting on the same qubit.
Expand All @@ -117,8 +120,9 @@ Note: Control qubit(s) followed by target qubit(s)
+ Controlled Z gate
- SqrtSwap i j
+ Not implemented yet
- CR i j phi
- CU i j phi
+ Controlled phase shift
+ You can also use "CR" instead of "CU"
- CCNot i j k
- CSwap i j k

Expand Down
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.1.0"
}
group = "com.dedztbh"
version = "1.5.3"
version = "1.5.4"

val projectRoot = "${group}.kuantum"
val projectRootExclude = "/${projectRoot.replace('.', '/')}"
Expand Down
12 changes: 12 additions & 0 deletions example/command/dft4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
SWAP 0 3
SWAP 1 2
H 3
CU 2 3 1.5707963267948966
CU 1 3 0.7853981633974483
CU 0 3 0.3926990816987242
H 2
CU 1 2 1.5707963267948966
CU 0 2 0.7853981633974483
H 1
CU 0 1 1.5707963267948966
H 0
File renamed without changes.
2 changes: 1 addition & 1 deletion teleport.txt → example/command/teleport.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Rot 0 22.5
Rot 0 1.0
H 1
CNot 1 2
CNot 0 1
Expand Down
16 changes: 16 additions & 0 deletions example/initmatrix/dft4.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0,0.25,0.0
0.25,0.0,0.23096988312782163,-0.0956708580912726,0.17677669529663687,-0.17677669529663692,0.0956708580912725,-0.23096988312782166,-4.592425496802574e-17,-0.25,-0.09567085809127258,-0.23096988312782163,-0.17677669529663692,-0.17677669529663687,-0.23096988312782168,-0.09567085809127242,-0.25,3.061616997868383e-17,-0.23096988312782168,0.09567085809127246,-0.17677669529663687,0.1767766952966369,-0.09567085809127243,0.23096988312782168,1.5308084989341915e-17,0.25,0.09567085809127246,0.23096988312782168,0.1767766952966369,0.17677669529663687,0.23096988312782168,0.09567085809127245
0.25,0.0,0.17677669529663687,-0.17677669529663692,-4.592425496802574e-17,-0.25,-0.17677669529663692,-0.17677669529663687,-0.25,3.061616997868383e-17,-0.17677669529663687,0.1767766952966369,1.5308084989341915e-17,0.25,0.1767766952966369,0.17677669529663687,0.25,0.0,0.17677669529663687,-0.17677669529663692,-4.592425496802574e-17,-0.25,-0.17677669529663692,-0.17677669529663687,-0.25,3.061616997868383e-17,-0.17677669529663687,0.1767766952966369,1.5308084989341915e-17,0.25,0.1767766952966369,0.17677669529663687
0.25,0.0,0.0956708580912725,-0.23096988312782166,-0.17677669529663692,-0.17677669529663687,-0.23096988312782168,0.09567085809127246,1.5308084989341915e-17,0.25,0.23096988312782168,0.09567085809127245,0.17677669529663687,-0.17677669529663692,-0.09567085809127258,-0.23096988312782163,-0.25,3.061616997868383e-17,-0.09567085809127243,0.23096988312782168,0.1767766952966369,0.17677669529663687,0.23096988312782163,-0.0956708580912726,-4.592425496802574e-17,-0.25,-0.23096988312782168,-0.09567085809127242,-0.17677669529663687,0.1767766952966369,0.09567085809127246,0.23096988312782168
0.25,0.0,-4.592425496802574e-17,-0.25,-0.25,3.061616997868383e-17,1.5308084989341915e-17,0.25,0.25,0.0,-4.592425496802574e-17,-0.25,-0.25,3.061616997868383e-17,1.5308084989341915e-17,0.25,0.25,0.0,-4.592425496802574e-17,-0.25,-0.25,3.061616997868383e-17,1.5308084989341915e-17,0.25,0.25,0.0,-4.592425496802574e-17,-0.25,-0.25,3.061616997868383e-17,1.5308084989341915e-17,0.25
0.25,0.0,-0.09567085809127258,-0.23096988312782163,-0.17677669529663687,0.1767766952966369,0.23096988312782168,0.09567085809127245,-4.592425496802574e-17,-0.25,-0.23096988312782168,0.09567085809127246,0.1767766952966369,0.17677669529663687,0.0956708580912725,-0.23096988312782166,-0.25,3.061616997868383e-17,0.09567085809127246,0.23096988312782168,0.17677669529663687,-0.17677669529663692,-0.23096988312782168,-0.09567085809127242,1.5308084989341915e-17,0.25,0.23096988312782163,-0.0956708580912726,-0.17677669529663692,-0.17677669529663687,-0.09567085809127243,0.23096988312782168
0.25,0.0,-0.17677669529663692,-0.17677669529663687,1.5308084989341915e-17,0.25,0.17677669529663687,-0.17677669529663692,-0.25,3.061616997868383e-17,0.1767766952966369,0.17677669529663687,-4.592425496802574e-17,-0.25,-0.17677669529663687,0.1767766952966369,0.25,0.0,-0.17677669529663692,-0.17677669529663687,1.5308084989341915e-17,0.25,0.17677669529663687,-0.17677669529663692,-0.25,3.061616997868383e-17,0.1767766952966369,0.17677669529663687,-4.592425496802574e-17,-0.25,-0.17677669529663687,0.1767766952966369
0.25,0.0,-0.23096988312782168,-0.09567085809127242,0.1767766952966369,0.17677669529663687,-0.09567085809127258,-0.23096988312782163,1.5308084989341915e-17,0.25,0.0956708580912725,-0.23096988312782166,-0.17677669529663687,0.1767766952966369,0.23096988312782163,-0.0956708580912726,-0.25,3.061616997868383e-17,0.23096988312782168,0.09567085809127245,-0.17677669529663692,-0.17677669529663687,0.09567085809127246,0.23096988312782168,-4.592425496802574e-17,-0.25,-0.09567085809127243,0.23096988312782168,0.17677669529663687,-0.17677669529663692,-0.23096988312782168,0.09567085809127246
0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17,0.25,0.0,-0.25,3.061616997868383e-17
0.25,0.0,-0.23096988312782168,0.09567085809127246,0.17677669529663687,-0.17677669529663692,-0.09567085809127243,0.23096988312782168,-4.592425496802574e-17,-0.25,0.09567085809127246,0.23096988312782168,-0.17677669529663692,-0.17677669529663687,0.23096988312782168,0.09567085809127245,-0.25,3.061616997868383e-17,0.23096988312782163,-0.0956708580912726,-0.17677669529663687,0.1767766952966369,0.0956708580912725,-0.23096988312782166,1.5308084989341915e-17,0.25,-0.09567085809127258,-0.23096988312782163,0.1767766952966369,0.17677669529663687,-0.23096988312782168,-0.09567085809127242
0.25,0.0,-0.17677669529663687,0.1767766952966369,-4.592425496802574e-17,-0.25,0.1767766952966369,0.17677669529663687,-0.25,3.061616997868383e-17,0.17677669529663687,-0.17677669529663692,1.5308084989341915e-17,0.25,-0.17677669529663692,-0.17677669529663687,0.25,0.0,-0.17677669529663687,0.1767766952966369,-4.592425496802574e-17,-0.25,0.1767766952966369,0.17677669529663687,-0.25,3.061616997868383e-17,0.17677669529663687,-0.17677669529663692,1.5308084989341915e-17,0.25,-0.17677669529663692,-0.17677669529663687
0.25,0.0,-0.09567085809127243,0.23096988312782168,-0.17677669529663692,-0.17677669529663687,0.23096988312782163,-0.0956708580912726,1.5308084989341915e-17,0.25,-0.23096988312782168,-0.09567085809127242,0.17677669529663687,-0.17677669529663692,0.09567085809127246,0.23096988312782168,-0.25,3.061616997868383e-17,0.0956708580912725,-0.23096988312782166,0.1767766952966369,0.17677669529663687,-0.23096988312782168,0.09567085809127246,-4.592425496802574e-17,-0.25,0.23096988312782168,0.09567085809127245,-0.17677669529663687,0.1767766952966369,-0.09567085809127258,-0.23096988312782163
0.25,0.0,1.5308084989341915e-17,0.25,-0.25,3.061616997868383e-17,-4.592425496802574e-17,-0.25,0.25,0.0,1.5308084989341915e-17,0.25,-0.25,3.061616997868383e-17,-4.592425496802574e-17,-0.25,0.25,0.0,1.5308084989341915e-17,0.25,-0.25,3.061616997868383e-17,-4.592425496802574e-17,-0.25,0.25,0.0,1.5308084989341915e-17,0.25,-0.25,3.061616997868383e-17,-4.592425496802574e-17,-0.25
0.25,0.0,0.09567085809127246,0.23096988312782168,-0.17677669529663687,0.1767766952966369,-0.23096988312782168,-0.09567085809127242,-4.592425496802574e-17,-0.25,0.23096988312782163,-0.0956708580912726,0.1767766952966369,0.17677669529663687,-0.09567085809127243,0.23096988312782168,-0.25,3.061616997868383e-17,-0.09567085809127258,-0.23096988312782163,0.17677669529663687,-0.17677669529663692,0.23096988312782168,0.09567085809127245,1.5308084989341915e-17,0.25,-0.23096988312782168,0.09567085809127246,-0.17677669529663692,-0.17677669529663687,0.0956708580912725,-0.23096988312782166
0.25,0.0,0.1767766952966369,0.17677669529663687,1.5308084989341915e-17,0.25,-0.17677669529663687,0.1767766952966369,-0.25,3.061616997868383e-17,-0.17677669529663692,-0.17677669529663687,-4.592425496802574e-17,-0.25,0.17677669529663687,-0.17677669529663692,0.25,0.0,0.1767766952966369,0.17677669529663687,1.5308084989341915e-17,0.25,-0.17677669529663687,0.1767766952966369,-0.25,3.061616997868383e-17,-0.17677669529663692,-0.17677669529663687,-4.592425496802574e-17,-0.25,0.17677669529663687,-0.17677669529663692
0.25,0.0,0.23096988312782168,0.09567085809127245,0.1767766952966369,0.17677669529663687,0.09567085809127246,0.23096988312782168,1.5308084989341915e-17,0.25,-0.09567085809127243,0.23096988312782168,-0.17677669529663687,0.1767766952966369,-0.23096988312782168,0.09567085809127246,-0.25,3.061616997868383e-17,-0.23096988312782168,-0.09567085809127242,-0.17677669529663692,-0.17677669529663687,-0.09567085809127258,-0.23096988312782163,-4.592425496802574e-17,-0.25,0.0956708580912725,-0.23096988312782166,0.17677669529663687,-0.17677669529663692,0.23096988312782163,-0.0956708580912726
File renamed without changes.
25 changes: 13 additions & 12 deletions src/main/kotlin/com/dedztbh/kuantum/ejml/operator/TFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,18 @@ open class TFinder(val config: Config, val scope: CoroutineScope) : Operator {
}
}

fun baseRMatrix(phi: Double) = CMatrix(
arrayOf(
doubleArrayOf(1.0, 0.0, 0.0, 0.0),
doubleArrayOf(0.0, 0.0, cos(phi), sin(phi))
)
)

@JvmField
val RCache: Array<MutableMap<Double, CMatrix>> = Array(N) { getHashMap() }
fun getRMatrix(i: Int, phi: Double): CMatrix {
if (cacheEnabled) RCache[i][phi]?.let { return it }
val RMat = CMatrix(
arrayOf(
doubleArrayOf(1.0, 0.0, 0.0, 0.0),
doubleArrayOf(0.0, 0.0, cos(phi), sin(phi))
)
)
return get0CtrlMatrix(i, RMat, false).also {
return get0CtrlMatrix(i, baseRMatrix(phi), false).also {
if (cacheEnabled) RCache[i][phi] = it
}
}
Expand Down Expand Up @@ -197,7 +198,7 @@ open class TFinder(val config: Config, val scope: CoroutineScope) : Operator {
}
getRotMatrix(i, angle)
}
"R" -> {
"R", "U" -> {
val phi = readDouble()
if (parallelMode) {
parallelMatrices[i] = getRMatrix(i, phi)
Expand All @@ -206,7 +207,7 @@ open class TFinder(val config: Config, val scope: CoroutineScope) : Operator {
getRMatrix(i, phi)
}
"CZ" -> get1CtrlMatrix(i, readInt(), Z)
"CR" -> get1CtrlMatrix(i, readInt(), getRMatrix(i, readDouble()), false)
"CR", "CU" -> get1CtrlMatrix(i, readInt(), baseRMatrix(readDouble()), false)
else -> map0Ctrl(cmd).let {
if (it != null) {
if (parallelMode) {
Expand Down Expand Up @@ -314,7 +315,7 @@ open class PTFinder(config: Config, scope: CoroutineScope) : TFinder(config, sco
}
async { getRotMatrix(i, angle) }
}
"R" -> {
"R", "U" -> {
val phi = readDouble()
if (parallelMode) {
parallelMatrices[i] = getRMatrix(i, phi)
Expand All @@ -326,10 +327,10 @@ open class PTFinder(config: Config, scope: CoroutineScope) : TFinder(config, sco
val j = readInt()
async { get1CtrlMatrix(i, j, Z) }
}
"CR" -> {
"CR", "CU" -> {
val j = readInt()
val phi = readDouble()
async { get1CtrlMatrix(i, j, getRMatrix(i, phi), false) }
async { get1CtrlMatrix(i, j, baseRMatrix(phi), false) }
}
else -> map0Ctrl(cmd).let {
if (it != null) {
Expand Down
26 changes: 14 additions & 12 deletions src/main/kotlin/com/dedztbh/kuantum/jblas/operator/TFinder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,19 @@ open class TFinder(val config: Config, val scope: CoroutineScope) : Operator {
}
}

/** Luckily, R_phi is symmetric */
fun baseRMatrix(phi: Double) = CMatrix(
2, 2,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, cos(phi), sin(phi)
)


@JvmField
val RCache: Array<MutableMap<Double, CMatrix>> = Array(N) { getHashMap() }
fun getRMatrix(i: Int, phi: Double): CMatrix {
if (cacheEnabled) RCache[i][phi]?.let { return it }
/** Luckily, R_phi is symmetric */
val RMat = CMatrix(
2, 2,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, cos(phi), sin(phi)
)
return get0CtrlMatrix(i, RMat, false).also {
return get0CtrlMatrix(i, baseRMatrix(phi), false).also {
if (cacheEnabled) RCache[i][phi] = it
}
}
Expand Down Expand Up @@ -199,7 +201,7 @@ open class TFinder(val config: Config, val scope: CoroutineScope) : Operator {
}
getRotMatrix(i, angle)
}
"R" -> {
"R", "U" -> {
val phi = readDouble()
if (parallelMode) {
parallelMatrices[i] = getRMatrix(i, phi)
Expand All @@ -208,7 +210,7 @@ open class TFinder(val config: Config, val scope: CoroutineScope) : Operator {
getRMatrix(i, phi)
}
"CZ" -> get1CtrlMatrix(i, readInt(), Z)
"CR" -> get1CtrlMatrix(i, readInt(), getRMatrix(i, readDouble()), false)
"CR", "CU" -> get1CtrlMatrix(i, readInt(), baseRMatrix(readDouble()), false)
else -> map0Ctrl(cmd).let {
if (it != null) {
if (parallelMode) {
Expand Down Expand Up @@ -313,7 +315,7 @@ open class PTFinder(config: Config, scope: CoroutineScope) : TFinder(config, sco
}
async { getRotMatrix(i, angle) }
}
"R" -> {
"R", "U" -> {
val phi = readDouble()
if (parallelMode) {
parallelMatrices[i] = getRMatrix(i, phi)
Expand All @@ -325,10 +327,10 @@ open class PTFinder(config: Config, scope: CoroutineScope) : TFinder(config, sco
val j = readInt()
async { get1CtrlMatrix(i, j, Z) }
}
"CR" -> {
"CR", "CU" -> {
val j = readInt()
val phi = readDouble()
async { get1CtrlMatrix(i, j, getRMatrix(i, phi), false) }
async { get1CtrlMatrix(i, j, baseRMatrix(phi), false) }
}
else -> map0Ctrl(cmd).let {
if (it != null) {
Expand Down

0 comments on commit 7d4503d

Please sign in to comment.