Skip to content

Commit

Permalink
Refactoring for IBM Q in native languages 🗣️
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNewJavaman committed Dec 7, 2021
1 parent 85cd77e commit f7cca8d
Show file tree
Hide file tree
Showing 35 changed files with 1,030 additions and 925 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
@file:OptIn(ExperimentalJsExport::class)

package net.javaman.brackt.api

import net.javaman.brackt.api.util.injections.InjectionAdder
import net.javaman.brackt.api.util.injections.InjectionManager
import net.javaman.brackt.api.util.logging.Logger
import net.javaman.brackt.api.util.properties.PropertyManager
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport
import kotlin.jvm.JvmStatic

@JsExport
class BracKtApi private constructor() {
companion object {
companion object : InjectionAdder {
/**
* Add injections for this module
*/
@JvmStatic
fun addInjections() = InjectionManager.add {
override fun addInjections() = InjectionManager.add {
many { Logger(it) }
one { PropertyManager() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ package net.javaman.brackt.api.quantum
* Custom providers can implement this interface and write extensions for [QuantumCircuit]
*/
interface QuantumGate {
data class Identity(val qubit: Int) : QuantumGate
data class PauliX(val qubit: Int) : QuantumGate
data class PauliY(val qubit: Int) : QuantumGate
data class PauliZ(val qubit: Int) : QuantumGate
data class Hadamard(val qubit: Int) : QuantumGate
data class S(val qubit: Int) : QuantumGate
data class SDagger(val qubit: Int) : QuantumGate
data class T(val qubit: Int) : QuantumGate
data class TDagger(val qubit: Int) : QuantumGate
data class Phase(val qubit: Int, val phi: Double) : QuantumGate
data class U(val qubit: Int, val theta: Double, val phi: Double, val lambda: Double) : QuantumGate
data class ControlledX(val controlQubit: Int, val targetQubit: Int) : QuantumGate
data class ControlledZ(val qubit1: Int, val qubit2: Int) : QuantumGate
data class Measure(val qubit: Int, val bit: Int) : QuantumGate
class Identity(val qubit: Int) : QuantumGate
class PauliX(val qubit: Int) : QuantumGate
class PauliY(val qubit: Int) : QuantumGate
class PauliZ(val qubit: Int) : QuantumGate
class Hadamard(val qubit: Int) : QuantumGate
class S(val qubit: Int) : QuantumGate
class SDagger(val qubit: Int) : QuantumGate
class T(val qubit: Int) : QuantumGate
class TDagger(val qubit: Int) : QuantumGate
class Phase(val qubit: Int, val phi: Double) : QuantumGate
class U(val qubit: Int, val theta: Double, val phi: Double, val lambda: Double) : QuantumGate
class ControlledX(val controlQubit: Int, val targetQubit: Int) : QuantumGate
class ControlledZ(val qubit1: Int, val qubit2: Int) : QuantumGate
class Measure(val qubit: Int, val bit: Int) : QuantumGate
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.javaman.brackt.api.util.injections

import net.javaman.brackt.api.util.logging.Logger
import net.javaman.brackt.api.util.reflection.getPlatformName
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
Expand Down Expand Up @@ -37,14 +38,16 @@ class InjectionManager {
}

inline fun <reified T : Any> many(noinline block: (KClass<*>) -> T) {
if (manyInstanceMap.keys.none { it == T::class }) manyInstanceMap[T::class] = block
if (manyInstanceMap.keys.none { it == T::class}) {
manyInstanceMap[T::class] = block
}
}

inline operator fun <reified T> getValue(thisRef: Any, property: KProperty<*>): T {
return manyInstanceMap.keys.firstOrNull { it == T::class }?.let {
manyInstanceMap[it]!!.invoke(thisRef::class) as T
} ?: oneInstanceList.firstOrNull { T::class.isInstance(it) } as? T
?: throw UninitializedInjectionException("")
?: throw UninitializedInjectionException("${T::class.getPlatformName()} has not yet been initialized")
}
}

Expand All @@ -54,3 +57,7 @@ class InjectionManager {
fun injection() = InjectionManager()

class UninitializedInjectionException(override val message: String) : Exception(message)

interface InjectionAdder {
fun addInjections()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.datetime.Clock
import net.javaman.brackt.api.util.formatters.PadDirection
import net.javaman.brackt.api.util.formatters.pretty
import net.javaman.brackt.api.util.formatters.withLength
import net.javaman.brackt.api.util.reflection.getPlatformQualifiedName
import net.javaman.brackt.api.util.reflection.getPlatformName
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport
import kotlin.js.JsName
Expand All @@ -28,7 +28,7 @@ class Logger(val className: String) {
val LEVEL_CHARS = LoggingLevel.values().maxOf { it.name.length }
}

@JsName("LoggerByClass") constructor(kClass: KClass<*>) : this(kClass.getPlatformQualifiedName())
@JsName("LoggerByClass") constructor(kClass: KClass<*>) : this(kClass.getPlatformName())

fun log(level: LoggingLevel, block: () -> Any) {
if (acceptableLevel.severity <= level.severity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package net.javaman.brackt.api.util.reflection

import kotlin.reflect.KClass

expect fun KClass<*>.getPlatformQualifiedName(): String
expect fun KClass<*>.getPlatformName(): String
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:OptIn(ExperimentalJsExport::class)

package net.javaman.brackt.api

// Due to a Kotlin/JS bug when compiling companion objects, we must redeclare this static method
@JsExport
fun addInjections() = BracKtApi.addInjections()
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import kotlin.reflect.KClass
/**
* Qualified name reflection is not supported by Kotlin/JS
*/
actual fun KClass<*>.getPlatformQualifiedName() = "brac-kt"
actual fun KClass<*>.getPlatformName() = this.simpleName ?: "Anonymous"
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package net.javaman.brackt.api.util.reflection

import kotlin.reflect.KClass

actual fun KClass<*>.getPlatformQualifiedName() = this.qualifiedName ?: "Anonymous"
actual fun KClass<*>.getPlatformName() = this.qualifiedName ?: "Anonymous"
1 change: 1 addition & 0 deletions brac-kt-ibmq-provider/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
kotlin("plugin.serialization")
id("org.jetbrains.dokka")
id("maven-publish")
id("io.gitlab.arturbosch.detekt")
}

repositories {
Expand Down
Loading

0 comments on commit f7cca8d

Please sign in to comment.