From c0848a940a646c835fd5183c1f9f6d17c4b0a4c0 Mon Sep 17 00:00:00 2001 From: DEDZTBH Date: Mon, 12 Oct 2020 13:52:19 +0800 Subject: [PATCH] v1.4.5 - fixed AllInit dimension error and newline printing problem (JBLAS) - better buildscript --- build.gradle.kts | 18 ++++----- .../kuantum/common/{main.kt => commonMain.kt} | 2 +- .../dedztbh/kuantum/common/matrix/const.kt | 9 +++++ .../kotlin/com/dedztbh/kuantum/ejml/main.kt | 4 +- .../com/dedztbh/kuantum/ejml/matrix/const.kt | 5 +-- .../kotlin/com/dedztbh/kuantum/jblas/main.kt | 4 +- .../dedztbh/kuantum/jblas/matrix/CMatrixIO.kt | 4 +- .../com/dedztbh/kuantum/jblas/matrix/const.kt | 40 ++++++++++--------- .../dedztbh/kuantum/EJMLBench.kt} | 2 + .../kotlin/com/dedztbh/kuantum/JBLASBench.kt | 30 ++++++++++++++ 10 files changed, 81 insertions(+), 37 deletions(-) rename src/main/kotlin/com/dedztbh/kuantum/common/{main.kt => commonMain.kt} (93%) create mode 100644 src/main/kotlin/com/dedztbh/kuantum/common/matrix/const.kt rename src/test/kotlin/{performance.kt => com/dedztbh/kuantum/EJMLBench.kt} (98%) create mode 100644 src/test/kotlin/com/dedztbh/kuantum/JBLASBench.kt diff --git a/build.gradle.kts b/build.gradle.kts index ba8215e..4742500 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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('.', '/')}" @@ -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")) @@ -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")) diff --git a/src/main/kotlin/com/dedztbh/kuantum/common/main.kt b/src/main/kotlin/com/dedztbh/kuantum/common/commonMain.kt similarity index 93% rename from src/main/kotlin/com/dedztbh/kuantum/common/main.kt rename to src/main/kotlin/com/dedztbh/kuantum/common/commonMain.kt index f41f722..d72ae8a 100644 --- a/src/main/kotlin/com/dedztbh/kuantum/common/main.kt +++ b/src/main/kotlin/com/dedztbh/kuantum/common/commonMain.kt @@ -10,7 +10,7 @@ import java.io.File * Project KuantumCircuitSim */ -fun main(args: Array, lib: String) { +fun commonMain(args: Array, lib: String) { val parser = ArgParser(CMD) val config = Config(parser) parser.parse(args) diff --git a/src/main/kotlin/com/dedztbh/kuantum/common/matrix/const.kt b/src/main/kotlin/com/dedztbh/kuantum/common/matrix/const.kt new file mode 100644 index 0000000..32bef11 --- /dev/null +++ b/src/main/kotlin/com/dedztbh/kuantum/common/matrix/const.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/com/dedztbh/kuantum/ejml/main.kt b/src/main/kotlin/com/dedztbh/kuantum/ejml/main.kt index ae67312..5ceed43 100644 --- a/src/main/kotlin/com/dedztbh/kuantum/ejml/main.kt +++ b/src/main/kotlin/com/dedztbh/kuantum/ejml/main.kt @@ -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) = main(args, "ejml") \ No newline at end of file +fun main(args: Array) = commonMain(args, "ejml") \ No newline at end of file diff --git a/src/main/kotlin/com/dedztbh/kuantum/ejml/matrix/const.kt b/src/main/kotlin/com/dedztbh/kuantum/ejml/matrix/const.kt index d3b43c1..d02af57 100644 --- a/src/main/kotlin/com/dedztbh/kuantum/ejml/matrix/const.kt +++ b/src/main/kotlin/com/dedztbh/kuantum/ejml/matrix/const.kt @@ -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( diff --git a/src/main/kotlin/com/dedztbh/kuantum/jblas/main.kt b/src/main/kotlin/com/dedztbh/kuantum/jblas/main.kt index 35e6b9a..4ec28d3 100644 --- a/src/main/kotlin/com/dedztbh/kuantum/jblas/main.kt +++ b/src/main/kotlin/com/dedztbh/kuantum/jblas/main.kt @@ -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 /** @@ -10,5 +10,5 @@ import org.jblas.util.Logger fun main(args: Array) { Logger.getLogger().setLevel(Logger.WARNING) - main(args, "jblas") + commonMain(args, "jblas") } \ No newline at end of file diff --git a/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/CMatrixIO.kt b/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/CMatrixIO.kt index b916fba..5af0d76 100644 --- a/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/CMatrixIO.kt +++ b/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/CMatrixIO.kt @@ -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() } @@ -48,6 +48,6 @@ fun CMatrix.printFancy( print(get(i, j)) if (j < columns - 1) print(", ") } - if (i < rows - 1) print("\n") + println() } }.flush() \ No newline at end of file diff --git a/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/const.kt b/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/const.kt index f172122..c7210e6 100644 --- a/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/const.kt +++ b/src/main/kotlin/com/dedztbh/kuantum/jblas/matrix/const.kt @@ -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 @@ -8,20 +9,20 @@ 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) @@ -29,39 +30,42 @@ 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() + diff --git a/src/test/kotlin/performance.kt b/src/test/kotlin/com/dedztbh/kuantum/EJMLBench.kt similarity index 98% rename from src/test/kotlin/performance.kt rename to src/test/kotlin/com/dedztbh/kuantum/EJMLBench.kt index 379cd5d..d7a4bcd 100644 --- a/src/test/kotlin/performance.kt +++ b/src/test/kotlin/com/dedztbh/kuantum/EJMLBench.kt @@ -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 diff --git a/src/test/kotlin/com/dedztbh/kuantum/JBLASBench.kt b/src/test/kotlin/com/dedztbh/kuantum/JBLASBench.kt new file mode 100644 index 0000000..10ef032 --- /dev/null +++ b/src/test/kotlin/com/dedztbh/kuantum/JBLASBench.kt @@ -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) + } +} \ No newline at end of file