Skip to content

Commit

Permalink
v0.2 implemented CSwap, add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
dedztbh committed Sep 22, 2020
1 parent b7aa665 commit b367fe5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ Where example.txt contains list of commands
Tester: Run a simulation on circuit with initial state |00...0>, then print the circuit's matrix and final state.

## Available Commands
I don't know how to implement CSwap yet, I will add it after I found out how :(

- Not i
- Hadamard i
- CNot i j
- Swap i j
- CCNot i j k
- CSwap i j k (Not implemented yet)
- CSwap i j k
- Z i
- S i
- T i
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.0.0"
}
group = "com.dedztbh"
version = "0.1"
version = "0.2"

val ejmlVersion = "0.39"

Expand Down
64 changes: 34 additions & 30 deletions src/main/kotlin/operator/Tester.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import kotlin.math.pow

class Tester(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 Zero = Matrix(N, N)
val IN2 = Ops.identity(jointStateSize)

/* Control Matrix generation based on this great article:
/** Control matrix generation based on this great article:
* http://www.sakkaris.com/tutorials/quantum_control_gates.html */
fun zeroCtrlMatrix(i: Int, mat: Matrix): Matrix {
fun get0CtrlMatrix(i: Int, mat: Matrix): Matrix {
var base0 = I1
repeat(N) {
val op =
Expand All @@ -33,7 +33,7 @@ class Tester(val N: Int) : Operator {
return base0
}

fun oneCtrlMatrix(i: Int, j: Int, mat: Matrix): Matrix {
fun get1CtrlMatrix(i: Int, j: Int, mat: Matrix): Matrix {
var base0 = I1
repeat(N) {
val op = if (it == i) KETBRA0 else I2
Expand All @@ -51,43 +51,47 @@ class Tester(val N: Int) : Operator {
return base0 + base1
}

var opMatrix = IN2
fun getCCNotMatrix(i: Int, j: Int, k: Int): Matrix {
/** using Sleator-Weinfurter construction
* matrix is actually reverse order as graph */
val cnotij = get1CtrlMatrix(i, j, NOT)
return get1CtrlMatrix(i, k, SQRT_NOT) *
cnotij *
get1CtrlMatrix(j, k, SQRT_NOT_DAG) *
cnotij *
get1CtrlMatrix(j, k, SQRT_NOT)
}


override fun runCmd(cmd: String): Int {
val i = readInt()
val newOp = when (cmd) {
"Not" -> zeroCtrlMatrix(i, NOT)
"Hadamard" -> zeroCtrlMatrix(i, H)
"CNot" -> oneCtrlMatrix(i, readInt(), NOT)
"Not" -> get0CtrlMatrix(i, NOT)
"Hadamard" -> get0CtrlMatrix(i, H)
"CNot" -> get1CtrlMatrix(i, readInt(), NOT)
"Swap" -> {
/* https://algassert.com/post/1717
* Swap implemented with 3 CNots */
val j = readInt()
val cnot0 = oneCtrlMatrix(i, j, NOT)
cnot0 * oneCtrlMatrix(j, i, NOT) * cnot0
val cnot0 = get1CtrlMatrix(i, j, NOT)
cnot0 * get1CtrlMatrix(j, i, NOT) * cnot0
}
"CCNot" -> {
// using Sleator-Weinfurter construction
"CCNot" -> getCCNotMatrix(i, readInt(), readInt())
"CSwap" -> {
/** https://quantumcomputing.stackexchange.com/
* questions/9342/how-to-implement-a-fredkin
* -gate-using-toffoli-and-cnots */
val j = readInt()
val k = readInt()
val cnotij = oneCtrlMatrix(i, j, NOT)
// matrix is actually reverse order as graph
oneCtrlMatrix(i, k, SQRT_NOT) *
cnotij *
oneCtrlMatrix(j, k, SQRT_NOT_DAG) *
cnotij *
oneCtrlMatrix(j, k, SQRT_NOT)
val cnotkj = get1CtrlMatrix(k, j, NOT)
cnotkj * getCCNotMatrix(i, j, k) * cnotkj
}
// "CSwap" -> {
// TODO: Implement CSwap
// Can anyone tell me how to implement CSwap :(
// }
"Z" -> zeroCtrlMatrix(i, Z)
"S" -> zeroCtrlMatrix(i, S)
"T" -> zeroCtrlMatrix(i, T)
"TDag" -> zeroCtrlMatrix(i, TDag)
"SqrtNot" -> zeroCtrlMatrix(i, SQRT_NOT)
"SqrtNotDag" -> zeroCtrlMatrix(i, SQRT_NOT_DAG)
"Z" -> get0CtrlMatrix(i, Z)
"S" -> get0CtrlMatrix(i, S)
"T" -> get0CtrlMatrix(i, T)
"TDag" -> get0CtrlMatrix(i, TDag)
"SqrtNot" -> get0CtrlMatrix(i, SQRT_NOT)
"SqrtNotDag" -> get0CtrlMatrix(i, SQRT_NOT_DAG)
// "SqrtSwap" -> {
// TODO: Implement SqrtSwap
// }
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/util/matrix.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import org.ejml.dense.row.CommonOps_ZDRM
* Created by DEDZTBH on 2020/09/22.
* Project KuantumCircuitSim
*/

/**
* Kronecker product of two ZDRM matrices
*/
infix fun ZMatrixRMaj.kron(B: ZMatrixRMaj) = let { A ->
val numColsC = A.numCols * B.numCols
val numRowsC = A.numRows * B.numRows
Expand All @@ -32,19 +36,26 @@ infix fun ZMatrixRMaj.kron(B: ZMatrixRMaj) = let { A ->
C
}

/**
* Convenient pure ops
*/
operator fun ZMatrixRMaj.plus(B: ZMatrixRMaj) =
ZMatrixRMaj(numRows, numCols).also { CommonOps_ZDRM.add(this, B, it) }

operator fun ZMatrixRMaj.times(B: ZMatrixRMaj) =
ZMatrixRMaj(numRows, B.numCols).also { CommonOps_ZDRM.mult(this, B, it) }


/**
* Useful constants
*/
typealias Matrix = ZMatrixRMaj
typealias Ops = CommonOps_ZDRM

/* 2^(-1/2) */
/** 2^(-1/2) */
const val HALF_AMPL = 0.70710678118654757273731092936941422522068023681640625

/* Don't change these constant matrices! */
/** Don't change these constant matrices! */
val NOT = Matrix(
arrayOf(
doubleArrayOf(0.0, 0.0, 1.0, 0.0),
Expand Down

0 comments on commit b367fe5

Please sign in to comment.