-
Notifications
You must be signed in to change notification settings - Fork 0
/
SigCompKet.scala
132 lines (77 loc) · 3.65 KB
/
SigCompKet.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package compket
import org.apache.commons.math.complex.Complex
import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution
/**
* Created by steve on 02/08/2018.
*/
class ComplexQbClass(real:Signal[Double], imaginary:Signal[Double], pendings:Set[ComplexQbClass]){
def getReal: Signal[Double] = real
def getImaginary: Signal[Double] = imaginary
def getPendings: Set[ComplexQbClass] = pendings
def degenerate(): QbComplex = {
if(real().isNaN || imaginary().isNaN ){
this.pendings.foreach(x=>x.degenerate())
}
println(toString)
QbComplex(new Complex(real(), imaginary()))
}
def +(that: ComplexQbClass): ComplexQbClass = new ComplexQbClass(Signal{real()+that.getReal()}, Signal{imaginary()+that.getImaginary()}, this.pendings++that.getPendings++Set(this, that))
def -(that: ComplexQbClass): ComplexQbClass = new ComplexQbClass(Signal{real()-that.getReal()}, Signal{imaginary()-that.getImaginary()}, this.pendings++that.getPendings++Set(this, that))
def *(that: ComplexQbClass): ComplexQbClass = new ComplexQbClass(Signal{real()*that.getReal()-imaginary()*that.getImaginary()}, Signal{real()*that.getImaginary()+imaginary()*that.getReal()}, this.pendings++that.getPendings++Set(this, that))
override def toString: String = {
real()+" + i"+imaginary()+ {
if (real().isNaN || imaginary().isNaN)
" (results not available since a part of the computation is not degenerated. Please, if you want to degenerate all quantums, call the degenerate() method)"
else ""
}
}
}
final case class QbComplex(value: Complex) extends ComplexQbClass(real = Signal{value.getReal},imaginary=Signal{value.getImaginary}, Set()) {
def this(real: Double, imaginary: Double) = this(new Complex(real, imaginary))
override def toString: String = value.getReal+" + i"+value.getImaginary
override def degenerate(): QbComplex = {
println(toString)
this
}
}
final case class CompKetClass(x:Double, y: Double, p: Array[Array[Double]]) extends ComplexQbClass(real = Signal{Double.NaN}, imaginary = Signal{Double.NaN}, Set()) {
require(p.length==2 && p.forall(_.length==2), "the probability matrice must be of size 2*2")
require(p.forall(_.sum==1.0), "the sum of probabilities of each line must be equal to 1 "+p.toList.map(_.toList))
override def degenerate(): QbComplex = {
val real:Signal[Double] = getReal
val imaginary:Signal[Double] = getImaginary
if(real().isNaN || imaginary().isNaN ) {
real() = this.x * new EnumeratedIntegerDistribution(Array(0, 1), p(0)).sample()
imaginary() = y * new EnumeratedIntegerDistribution(Array(0, 1), p(1)).sample()
}
println(toString)
QbComplex(new Complex(real(), imaginary()))
}
override def toString: String = {
val real:Signal[Double] = getReal
val imaginary:Signal[Double] = getImaginary
if(real().isNaN || imaginary().isNaN ) {
if(x==0 && y == 0) "0" else (if(x!=0) x + " (" + "%.2f".format(p(0)(0)) + "|0> + " + "%.2f".format(p(0)(1)) + "|1>)"+(if(y!=0) " + " else "") else "")+(if(y!=0) "i"+y+"("+"%.2f".format(p(1)(0))+"|0> + "+"%.2f".format(p(1)(1))+"|1>)" else "")
}
else{
real()+" + i"+imaginary()
}
}
}
object SigCompKet {
def run(): Unit = {
val a = CompKetClass(1, 2, Array(Array(0.4, 0.6), Array(0.5, 0.5)))
val b = CompKetClass(1.1, 2.1, Array(Array(0.12, 0.88), Array(0.22, 0.78)))
val sum: ComplexQbClass = a+b
println("a = "+a)
println("b = "+b)
println("sum = "+sum)
//val ar = a.degenerate()
//val br = b.degenerate()
val sumr = sum.degenerate()
println("a = "+a)
println("b = "+b)
println("sum = "+sum)
}
run()
}