Last active
January 13, 2025 19:50
-
-
Save daxfohl/50ca6e79072762b9a18744478d8aaf95 to your computer and use it in GitHub Desktop.
quantum.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tableau {} | |
class SV {} | |
class DM {} | |
class Stabilizer {} | |
class Unitary {} | |
class Channel {} | |
fun apply(stabilizer: Stabilizer, tableau: Tableau) {} | |
fun apply(unitary: Unitary, sv: SV) {} | |
fun apply(channel: Channel, dm: DM) {} | |
fun toChannel(unitary: Unitary): Channel = Channel() | |
fun toUnitary(stabilizer: Stabilizer): Unitary = Unitary() | |
interface IIndexed<out TQReg> { | |
fun mapIndices(indices: Map<Int, Int>): TQReg | |
} | |
interface Gate<in TQReg> { | |
val name: String | |
} | |
data class Op<TQReg>(val gate: Gate<TQReg>, val qreg: TQReg) | |
fun <TQReg> Gate<TQReg>.on(qreg: TQReg): Op<TQReg> { | |
return Op(this, qreg) | |
} | |
data class GateBuilder<TData, out TGate>(var data: TData, val build: (data: TData) -> TGate) | |
fun <TData, TGate> GateBuilder<TData, TGate>.build(): TGate { | |
return this.build(this.data) | |
} | |
interface IExponentiable<out TGate> { | |
fun exponentiate(exp: Double): TGate | |
} | |
interface IRepeatable<out TGate> { | |
fun exponentiate(exp: Int): TGate | |
} | |
interface IChannelGate<in TQReg> : Gate<TQReg> { | |
val channel: Channel | |
fun apply(dm: DM) { | |
apply(channel, dm) | |
} | |
} | |
interface IUnitaryGate<in TQReg> : IChannelGate<TQReg> { | |
val unitary: Unitary | |
fun apply(sv: SV) { | |
apply(unitary, sv) | |
} | |
override val channel: Channel get() = toChannel(unitary) | |
} | |
interface IStabilizerGate<in TQReg> : IUnitaryGate<TQReg> { | |
val stabilizer: Stabilizer | |
fun apply(tableau: Tableau) { | |
apply(stabilizer, tableau) | |
} | |
override val unitary: Unitary get() = toUnitary(stabilizer) | |
} | |
data class EigenData(var exp: Double) | |
interface IEigenGate<in TQReg, out TGate> : IUnitaryGate<TQReg>, IExponentiable<TGate> where TGate : IEigenGate<TQReg, TGate> { | |
fun builder(): GateBuilder<EigenData, TGate> | |
override fun exponentiate(exp: Double): TGate { | |
val b = builder() | |
b.data.exp *= exp | |
return b.build() | |
} | |
} | |
interface IPauliGate<in TQReg, out TGate, out TBaseGate> : IStabilizerGate<TQReg>, IRepeatable<TGate>, IExponentiable<TBaseGate> | |
class EigenX(val exp: Double = 1.0) : IEigenGate<Int, EigenX> { | |
override val name = "X" | |
override val unitary = Unitary() | |
override fun builder() = GateBuilder(EigenData(exp)) { EigenX(it.exp) } | |
} | |
class X(val repetitions: Int, val index: Int) : IPauliGate<X, EigenX> { | |
override val name get() = "PauliX" | |
override val stabilizer = Stabilizer() | |
override val indices = intArrayOf(index) | |
override fun exponentiate(exp: Double) = EigenX(exp * repetitions, index) | |
override fun exponentiate(exp: Int) = X(exp * repetitions, index) | |
} | |
class EigenY(val exp: Double = 1.0) : IEigenGate<Int, EigenY> { | |
override val name = "Y" | |
override val unitary = Unitary() | |
override fun builder() = GateBuilder(EigenData(exp)) { EigenY(it.exp) } | |
} | |
// Single class hierarchy. Need to decide which constructor is more important | |
// ...exponent or single-qubit? Eigen or SQ? | |
// But everything needs qubits. So, | |
fun main() { | |
println("Hello World!") | |
val x1 = EigenX(2.0)as IEigenGate<Int, *> | |
val x2 = x1.exponentiate(2.0) | |
val x3 = x2.builder() | |
val xs = listOf(EigenX(2.0), EigenY(2.4)) | |
val ys = xs.map { it.exponentiate(3.0) as IEigenGate<Int, *> } | |
val x = ys.get(0) | |
val y = ys.get(1) | |
println(x) | |
println(y) | |
val xb = x.builder() | |
xb.data.exp | |
} | |
open class QID{} | |
class AwesomeQID: QID(){} | |
open class G(override val name: String): Gate<QID>{} | |
class AwesomeG(override val name: String): Gate<AwesomeQID>{} | |
class AltG(override val name: String): G(name){} | |
fun asodifj() { | |
val q = QID() | |
val aq = AwesomeQID() | |
val g = G("a") | |
val ga = AwesomeG("b") | |
val galt = AltG("c") | |
g.on(q) | |
g.on(aq) | |
ga.on(q) | |
ga.on(aq) | |
galt.on(q) | |
galt.on(aq) | |
Op(g, q) | |
Op(g, aq) | |
Op(ga, q) | |
Op(ga, aq) | |
Op(galt, q) | |
Op(galt, aq) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment