Skip to content

Commit

Permalink
v1.4.5
Browse files Browse the repository at this point in the history
- fixed AllInit dimension error and newline printing problem (JBLAS)
- better buildscript
  • Loading branch information
dedztbh committed Oct 12, 2020
1 parent 0a2033d commit c0848a9
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 37 deletions.
18 changes: 9 additions & 9 deletions 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 = "1.4.4"
version = "1.4.5"

val projectRoot = "${group}.kuantum"
val projectRootExclude = "/${projectRoot.replace('.', '/')}"
Expand Down Expand Up @@ -52,10 +52,10 @@ tasks {
register("shadowJarEjml", ShadowJar::class) {
archiveBaseName.set("Kuantum-ejml")
from(sourceSets.main.get().output)
configurations.add(project.configurations.runtime.get())
configurations.add(project.configurations.implementation.get().apply {
isCanBeResolved = true
})
configurations = listOf(
project.configurations.runtime.get(),
project.configurations.runtimeClasspath.get()
)
mergeServiceFiles()
manifest {
attributes(mapOf("Main-Class" to "${projectRoot}.ejml.MainKt"))
Expand All @@ -70,10 +70,10 @@ tasks {
register("shadowJarJBLAS", ShadowJar::class) {
archiveBaseName.set("Kuantum-jblas")
from(sourceSets.main.get().output)
configurations.add(project.configurations.runtime.get())
configurations.add(project.configurations.implementation.get().apply {
isCanBeResolved = true
})
configurations = listOf(
project.configurations.runtime.get(),
project.configurations.runtimeClasspath.get()
)
mergeServiceFiles()
manifest {
attributes(mapOf("Main-Class" to "${projectRoot}.jblas.MainKt"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.io.File
* Project KuantumCircuitSim
*/

fun main(args: Array<String>, lib: String) {
fun commonMain(args: Array<String>, lib: String) {
val parser = ArgParser(CMD)
val config = Config(parser)
parser.parse(args)
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/dedztbh/kuantum/common/matrix/const.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dedztbh.kuantum.common.matrix

/**
* Created by DEDZTBH on 2020/10/12.
* Project KuantumCircuitSim
*/

/** 2^(-1/2) */
const val HALF_AMPL = 0.70710678118654757273731092936941422522068023681640625
4 changes: 2 additions & 2 deletions src/main/kotlin/com/dedztbh/kuantum/ejml/main.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.dedztbh.kuantum.ejml

import com.dedztbh.kuantum.common.main
import com.dedztbh.kuantum.common.commonMain

/**
* Created by DEDZTBH on 2020/10/11.
* Project KuantumCircuitSim
*/

fun main(args: Array<String>) = main(args, "ejml")
fun main(args: Array<String>) = commonMain(args, "ejml")
5 changes: 2 additions & 3 deletions src/main/kotlin/com/dedztbh/kuantum/ejml/matrix/const.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.dedztbh.kuantum.ejml.matrix

import com.dedztbh.kuantum.common.matrix.HALF_AMPL

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

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

/** Don't change these constant matrices! */
val NOT = CMatrix(
arrayOf(
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/dedztbh/kuantum/jblas/main.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dedztbh.kuantum.jblas

import com.dedztbh.kuantum.common.main
import com.dedztbh.kuantum.common.commonMain
import org.jblas.util.Logger

/**
Expand All @@ -10,5 +10,5 @@ import org.jblas.util.Logger

fun main(args: Array<String>) {
Logger.getLogger().setLevel(Logger.WARNING)
main(args, "jblas")
commonMain(args, "jblas")
}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/CMatrixIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun CMatrix.toStringFancy(
s.append(get(i, j))
if (j < columns - 1) s.append(", ")
}
if (i < rows - 1) s.append("\n")
s.append("\n")
}
return s.toString()
}
Expand All @@ -48,6 +48,6 @@ fun CMatrix.printFancy(
print(get(i, j))
if (j < columns - 1) print(", ")
}
if (i < rows - 1) print("\n")
println()
}
}.flush()
40 changes: 22 additions & 18 deletions src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/const.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dedztbh.kuantum.jblas.matrix

import com.dedztbh.kuantum.common.matrix.HALF_AMPL
import org.jblas.ComplexDoubleMatrix
import org.jblas.DoubleMatrix

Expand All @@ -8,60 +9,63 @@ import org.jblas.DoubleMatrix
* Project KuantumCircuitSim
*/

/** 2^(-1/2) */
const val HALF_AMPL = 0.70710678118654757273731092936941422522068023681640625
/** Don't change these constant matrices!
* JBLAS is column-major so have to transpose non-symmetric matrices */

/** Don't change these constant matrices! */
/** Don't need transpose for vectors and symmetric matrices */
val NOT: ComplexDoubleMatrix = CMatrix(
2, 2,
0.0, 0.0, 1.0, 0.0,
1.0, 0.0, 0.0, 0.0
).transpose()
)
val H: ComplexDoubleMatrix = CMatrix(
2, 2,
HALF_AMPL, 0.0, HALF_AMPL, 0.0,
HALF_AMPL, 0.0, -HALF_AMPL, 0.0
).transpose()
)
val I1 = CMatrix(DoubleMatrix.eye(1), null)
val I2 = CMatrix(DoubleMatrix.eye(2), null)

val KET0: ComplexDoubleMatrix = CMatrix(
2, 1,
1.0, 0.0,
0.0, 0.0
).transpose()
)
val KET1: ComplexDoubleMatrix = CMatrix(
2, 1,
0.0, 0.0,
1.0, 0.0
).transpose()
)

val KETBRA0: ComplexDoubleMatrix = CMatrix.diag(CMatrix(doubleArrayOf(1.0, 0.0, 0.0, 0.0)).transpose())
val KETBRA1: ComplexDoubleMatrix = CMatrix.diag(CMatrix(doubleArrayOf(0.0, 0.0, 1.0, 0.0)).transpose())
val KETBRA0: ComplexDoubleMatrix = CMatrix.diag(CMatrix(doubleArrayOf(1.0, 0.0, 0.0, 0.0)))
val KETBRA1: ComplexDoubleMatrix = CMatrix.diag(CMatrix(doubleArrayOf(0.0, 0.0, 1.0, 0.0)))
val SQRT_NOT: ComplexDoubleMatrix = CMatrix(
2, 2,
0.5, 0.5, 0.5, -0.5,
0.5, -0.5, 0.5, 0.5
).transpose()
)
val SQRT_NOT_DAG: ComplexDoubleMatrix = SQRT_NOT.hermitian()
val Y: ComplexDoubleMatrix = CMatrix(
2, 2,
0.0, 0.0, 0.0, -1.0,
0.0, 1.0, 0.0, 0.0
).transpose()
val Z: ComplexDoubleMatrix = CMatrix(
2, 2,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, -1.0, 0.0
).transpose()
)
val S: ComplexDoubleMatrix = CMatrix(
2, 2,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0
).transpose()
)
val T: ComplexDoubleMatrix = CMatrix(
2, 2,
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, HALF_AMPL, HALF_AMPL
).transpose()
)
val TDag: ComplexDoubleMatrix = T.hermitian()

/** These need transpose */
val Y: ComplexDoubleMatrix = CMatrix(
2, 2,
0.0, 0.0, 0.0, -1.0,
0.0, 1.0, 0.0, 0.0
).transpose()

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.dedztbh.kuantum

import org.ejml.data.CMatrixRMaj
import org.ejml.data.Complex_F32
import org.ejml.data.Complex_F64
Expand Down
30 changes: 30 additions & 0 deletions src/test/kotlin/com/dedztbh/kuantum/JBLASBench.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dedztbh.kuantum

import com.lukaskusik.coroutines.transformations.reduce.reduceParallel
import kotlinx.coroutines.runBlocking
import org.jblas.ComplexDoubleMatrix
import org.jblas.DoubleMatrix
import kotlin.system.measureTimeMillis

/**
* Created by DEDZTBH on 2020/10/08.
* Project KuantumCircuitSim
*/

fun main() {
// Slower than numpy, I'm not happy :(
val N = 1000
val mat = ComplexDoubleMatrix(DoubleMatrix.rand(N, N), DoubleMatrix.rand(N, N))
measureTimeMillis {
// repeat(500) {
// mat.mmul(mat)
// }
runBlocking {
List(500) { mat }.reduceParallel { d1, d2 ->
d1.mmul(d2)
}
}
}.let {
println(it.toDouble() / 1000.0)
}
}

0 comments on commit c0848a9

Please sign in to comment.