-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
# CMU_Coin-flipping_Experience | ||
|
||
A Coin Flipping machine. | ||
A Coin Flipping machine inspired by the IBM Q Experience. | ||
|
||
Usage: java -jar xxx.jar \<input file> \<operator> \<coin count (default 5)> | ||
15-459 Assignment | ||
|
||
## Usage | ||
|
||
java -jar xxx.jar \<input file> \<operator> \<coin count (default 5)> | ||
|
||
|
||
## Available operators: | ||
|
||
Available operators: | ||
Tester: Run a simulation and print results | ||
Prob0Init: Print the probabilities of each possible outcome | ||
|
||
Input commands: | ||
$$ | ||
\begin{align*} | ||
\text{Flip} & & i & & \text{(randomly set coin~$i$ to $0$ or $1$ with probability $1/2$ each)} \\ | ||
\text{Not} & & i & & \text{(turn over the $i$th coin; i.e., deterministically reverse its $0$/$1$ status)} \\ | ||
\text{CNot} & & i\ j & & \text{(if coin~$i$ is $1$ (Tails) then do a Not on coin~$j$, else do nothing)}\\ | ||
\text{CSwap} & & i\ j\ k & & \text{(if coin~$i$ is $1$ (Tails) then swap the values of coins $j$ and $k$)} | ||
\end{align*} | ||
\begin{align*} | ||
\text{CCNot} & & i\ j \ k& & \text{(if coins~$i$ and $j$ are \emph{both} $1$ then do `Not~$k$', else do nothing)}\\ | ||
\text{GenFlip} & & i\ p & & \text{(set coin~$i$ to $0$ with probability $1-p$, to $1$ with probability $p$)} \\ | ||
\text{Gen1Bit} & & i\ p\ q & & \text{(if coin~$i$ is $0$ then make it~$1$ with probability $p$,\qquad}\\ | ||
& & & & \text{else if coin~$i$ is $1$ then make it~$0$ with probability $q$)} | ||
\end{align*} | ||
$$ | ||
|
||
Prob0Init: Print the probabilities of each possible outcome (0 initialized) | ||
|
||
ProbAllInit: Print the probabilities of each coin being 1 for all initializations | ||
|
||
|
||
## Input commands: | ||
|
||
``` | ||
$$ \begin{align*} | ||
\text{Flip} & & i & & \text{(randomly set coin~$i$ to $0$ or $1$ with probability $1/2$ each)} \\ | ||
\text{Not} & & i & & \text{(turn over the $i$th coin; i.e., deterministically reverse its $0$/$1$ status)} \\ | ||
\text{CNot} & & i\ j & & \text{(if coin~$i$ is $1$ (Tails) then do a Not on coin~$j$, else do nothing)}\\ | ||
\text{CSwap} & & i\ j\ k & & \text{(if coin~$i$ is $1$ (Tails) then swap the values of coins $j$ and $k$)} | ||
\end{align*} | ||
\begin{align*} | ||
\text{CCNot} & & i\ j \ k& & \text{(if coins~$i$ and $j$ are \emph{both} $1$ then do `Not~$k$', else do nothing)}\\ | ||
\text{GenFlip} & & i\ p & & \text{(set coin~$i$ to $0$ with probability $1-p$, to $1$ with probability $p$)} \\ | ||
\text{Gen1Bit} & & i\ p\ q & & \text{(if coin~$i$ is $0$ then make it~$1$ with probability $p$,\qquad}\\ | ||
& & & & \text{else if coin~$i$ is $1$ then make it~$0$ with probability $q$)} | ||
\end{align*} $$ | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
GenFlip 0 0.1 | ||
GenFlip 1 0.3 | ||
GenFlip 2 0.5 | ||
GenFlip 3 0.7 | ||
GenFlip 4 0.9 | ||
CNot 1 2 | ||
CSwap 3 4 0 | ||
CCNot 1 2 3 | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package operator | ||
|
||
import allStates | ||
import org.nd4j.linalg.api.buffer.DataType | ||
import org.nd4j.linalg.api.ndarray.INDArray | ||
import org.nd4j.linalg.factory.Nd4j | ||
|
||
/** | ||
* Created by DEDZTBH on 2020/09/15. | ||
* Project CMU_Coin-flipping_Experience | ||
*/ | ||
class ProbAllInit(N: Int) : ProbFinder(N) { | ||
fun eval(probs: INDArray): INDArray { | ||
operations.forEach { | ||
it.apply { | ||
when (this) { | ||
is Matrix -> { | ||
probs.muliRowVector(opVec) | ||
probs.addiRowVector(opBias) | ||
} | ||
is CNot -> { | ||
val i = i.toLong() | ||
val j = j.toLong() | ||
val x = probs.getColumn(i) | ||
val y = probs.getColumn(j) | ||
//(1.0 - x) * y + x * (1.0 - y) | ||
val newCol = x.mul(y).mul(-2.0).add(x).add(y) | ||
probs.putColumn(this.j, newCol) | ||
} | ||
is CSwap -> { | ||
val i = i.toLong() | ||
val j = j.toLong() | ||
val k = k.toLong() | ||
val x = probs.getColumn(i) | ||
val y = probs.getColumn(j) | ||
val z = probs.getColumn(k) | ||
val xy = x.mul(y) | ||
val xz = x.mul(z) | ||
//(1.0 - x) * y + x * z | ||
probs.putColumn(this.j, y.sub(xy).add(xz)) | ||
//(1.0 - x) * z + x * y | ||
probs.putColumn(this.k, z.sub(xz).add(xy)) | ||
} | ||
is CCNot -> { | ||
val i = i.toLong() | ||
val j = j.toLong() | ||
val k = k.toLong() | ||
val x = probs.getColumn(i) | ||
val y = probs.getColumn(j) | ||
val z = probs.getColumn(k) | ||
//(1.0 - x) * (1.0 - y) | ||
val probBoth0 = x.mul(y).sub(x).sub(y).add(1.0) | ||
//probBoth0 * z + (1.0 - probBoth0) * (1.0 - z) | ||
probs.putColumn(this.k, probBoth0.mul(z).mul(2.0).sub(probBoth0).sub(z).add(1.0)) | ||
} | ||
is Gen1Bit -> { | ||
val i = i.toLong() | ||
val x = probs.getColumn(i) | ||
val xx = x.mul(x) | ||
val p_comp = 1.0 - p | ||
probs.putColumn( | ||
this.i, | ||
//x * (1.0 - q) * x + (1.0 - x) * (p + (1.0 - p) * x) | ||
//= xx(1-q) + p + (1-p)x - xp - xx(1-p) | ||
xx.mul(1.0 - q).add(p).add(x.mul(p_comp)).sub(x.mul(p)).sub(xx.mul(p_comp)) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
return probs | ||
} | ||
|
||
override fun printResult() { | ||
val probs = eval( | ||
// a 2^n by n matrix | ||
Nd4j.create(allStates(N)).castTo(DataType.DOUBLE) | ||
) | ||
println(probs) | ||
} | ||
} |
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