Java源码示例:com.gluonhq.strange.Result
示例1
public static void main(String[] args) {
QuantumExecutionEnvironment simulator = new SimpleQuantumExecutionEnvironment();
Program program = new Program(1);
Step step0 = new Step();
step0.addGate(new X(0));
Step step1 = new Step();
step1.addGate(new X(0));
program.addStep(step0);
program.addStep(step1);
program.initializeQubit(0,.5);
Result result = simulator.runProgram(program);
Qubit[] qubits = result.getQubits();
Renderer.showProbabilities(program,1000);
Renderer.renderProgram(program);
}
示例2
static void try00() {
QuantumExecutionEnvironment simulator = new SimpleQuantumExecutionEnvironment();
Program program = new Program(2);
Step oracleStep = new Step();
Oracle oracle = createOracle();
oracleStep.addGate(oracle);
program.addStep(oracleStep);
Step probStep = new Step();
probStep.addGate(new ProbabilitiesGate(0));
program.addStep(probStep);
Result result = simulator.runProgram(program);
Qubit[] qubits = result.getQubits();
System.err.println("measured = "+ qubits[0].measure() + ", " + qubits[1].measure());
Renderer.renderProgram(program);
}
示例3
static void try11() {
QuantumExecutionEnvironment simulator = new SimpleQuantumExecutionEnvironment();
Program program = new Program(2);
Step prepareStep = new Step();
prepareStep.addGate(new X(0));
prepareStep.addGate(new X(1));
program.addStep(prepareStep);
Step oracleStep = new Step();
Oracle oracle = createOracle();
oracleStep.addGate(oracle);
program.addStep(oracleStep);
Result result = simulator.runProgram(program);
Qubit[] qubits = result.getQubits();
System.err.println(", measured = "
+ qubits[0].measure() + ", " + qubits[1].measure());
}
示例4
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("Hello, demo");
Program p = new Program(4);
Gate yGate = new Y(0);
Gate xGate = new X(1);
Gate zGate = new Z(3);
Step step = new Step();
step.addGates(yGate, xGate, zGate);
p.addStep(step);
SimpleQuantumExecutionEnvironment sqee = new SimpleQuantumExecutionEnvironment();
Result res = sqee.runProgram(p);
Qubit[] qubits = res.getQubits();
Arrays.asList(qubits).forEach(q -> System.out.println(q.measure()));
Arrays.asList(res.getProbability()).forEach(c -> System.out.println("prob = "+c));
Complex[][] perm = sqee.createPermutationMatrix(1,2,3);
for (int i = 0; i < perm.length; i++) {
StringBuffer sb = new StringBuffer();
for (int j = 0; j < perm[i].length; j++) {
sb.append(perm[i][j]).append(" ");
}
System.out.println("sb = "+sb);
}
PermutationGate pg = new PermutationGate(0,2,3);
Complex[][] m = pg.getMatrix();
printMatrix(m);
}
示例5
private void calculate() {
int nq = model.getNQubits();
Program p = new Program(nq);
int nsteps = model.getNumberOfSteps();
for (int step = 0; step < nsteps; step++) {
Step s = new Step();
Gate[] gate = model.getGatesByStep(step);
for (Gate g : gate) {
s.addGate(g);
}
p.addStep(s);
SimpleQuantumExecutionEnvironment sqee = new SimpleQuantumExecutionEnvironment();
Result res = sqee.runProgram(p);
Qubit[] qubits = res.getQubits();
List<Double> probability = new ArrayList<>();
for (Qubit qubit: qubits) {
probability.add(qubit.getProbability());
}
ObservableList<Double> endStates = model.getEndStates();
endStates.setAll(probability);
}
}
示例6
@Test
public void fourierAdditionProgram0101() {
Step prep = new Step(new X(0), new X(2));
Program p = new Program(4,
prep,
new Step(new Fourier(2, 0)),
new Step (new Cr(0,3, 2, 1)),
new Step (new Cr(0,2, 2, 2)),
new Step (new Cr(1,2, 2, 1)),
new Step(new InvFourier(2,0)));
Result res = runProgram(p);
Complex[] probability = res.getProbability();
Complex.printArray(probability);
Qubit[] qubits = res.getQubits();
assertEquals(0, qubits[0].measure());
assertEquals(1, qubits[1].measure());
assertEquals(1, qubits[2].measure());
assertEquals(0, qubits[3].measure());
}
示例7
@Test
public void fourierAdditionProgram1010() {
Step prep = new Step(new X(1), new X(3));
Program p = new Program(4,
prep,
new Step(new Fourier(2, 0)),
new Step (new Cr(0,3, 2, 1)),
new Step (new Cr(0,2, 2, 2)),
new Step (new Cr(1,2, 2, 1)),
new Step(new InvFourier(2,0)));
Result res = runProgram(p);
Complex[] probability = res.getProbability();
Complex.printArray(probability);
Qubit[] qubits = res.getQubits();
assertEquals(0, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(0, qubits[2].measure());
assertEquals(1, qubits[3].measure());
}
示例8
@Test
public void fourierAdditionProgram0011() {
Step prep = new Step(new X(2), new X(3));
Program p = new Program(4,
prep,
new Step(new Fourier(2, 0)),
new Step (new Cr(1,2, 2, 1)),
new Step (new Cr(0,2, 2, 2)),
new Step (new Cr(0,3, 2, 1)),
new Step(new InvFourier(2,0)));
Result res = runProgram(p);
Complex[] probability = res.getProbability();
Complex.printArray(probability);
Qubit[] qubits = res.getQubits();
assertEquals(1, qubits[0].measure());
assertEquals(1, qubits[1].measure());
assertEquals(1, qubits[2].measure());
assertEquals(1, qubits[3].measure());
}
示例9
private void simulate() {
Program p = new Program(2);
Step s1 = new Step();
s1.addGate(new X(0));
p.addStep(s1);
Step s2 = new Step();
s2.addGate(new X(1));
p.addStep(s2);
SimpleQuantumExecutionEnvironment sqee = new SimpleQuantumExecutionEnvironment();
Result res = sqee.runProgram(p);
Qubit[] qubits = res.getQubits();
RenderEngine sim = RenderEngine.createNode(p);
borderPane.setCenter(sim);
Button button = new Button("uh?");
//MaterialIconFactory
// .get()
// .createIconButton(MaterialIcon.PLAY_ARROW, "", "2em", "", ContentDisplay.GRAPHIC_ONLY);
button.setOnAction(e -> sim.animate());
borderPane.setTop(button);
}
示例10
@Test
public void initializeFifty() {
Program p = new Program(1, new Step(new Identity(0)));
double sq = Math.sqrt(.5);
p.initializeQubit(0, sq);
int cnt = 0;
boolean got0 = false;
boolean got1 = false;
int i = 0;
while (i < 100 && !(got0 && got1)) {
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
int q0 = qubits[0].measure();
if (q0 == 0) {
got0 = true;
}
if (q0 == 1) {
got1 = true;
}
i++;
}
assertTrue(got0);
assertTrue(got1);
}
示例11
@Test
public void initializeLowHigh() {
Program p = new Program(1, new Step(new Identity(0)));
double sq = Math.sqrt(.1);
p.initializeQubit(0, sq);
for (int c = 0; c < 10; c++){
int cnt0 = 0;
int cnt1 = 1;
int i = 0;
while (i < 100) {
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
int q0 = qubits[0].measure();
if (q0 == 0) {
cnt0++;
}
if (q0 == 1) {
cnt1++;
}
i++;
}
assertTrue(cnt1 > cnt0);
}
}
示例12
public static void main(String[] args) {
QuantumExecutionEnvironment simulator = new SimpleQuantumExecutionEnvironment();
Random random = new Random();
Program program = new Program(2);
Step step1 = new Step();
step1.addGate(new Hadamard(1));
Complex[][] matrix = new Complex[][]{
{Complex.ONE,Complex.ZERO,Complex.ZERO,Complex.ZERO},
{Complex.ZERO,Complex.ONE,Complex.ZERO,Complex.ZERO},
{Complex.ZERO,Complex.ZERO,Complex.ZERO,Complex.ONE},
{Complex.ZERO,Complex.ZERO,Complex.ONE,Complex.ZERO}
};
Oracle oracle = new Oracle(matrix);
Step step2 = new Step();
step2.addGate(oracle);
program.addStep(step1);
program.addStep(step2);
Result result = simulator.runProgram(program);
Renderer.showProbabilities(program,1000);
Renderer.renderProgram(program);
}
示例13
public static int randomBit() {
Program program = new Program(1, new Step(new Hadamard(0)));
QuantumExecutionEnvironment qee = new SimpleQuantumExecutionEnvironment();
Result result = qee.runProgram(program);
Qubit[] qubits = result.getQubits();
int answer = qubits[0].measure();
return answer;
}
示例14
@Test
public void ToffoliGate0() {
// |000> -> |000>
Program p = new Program(3, new Step(new Toffoli(2,1,0)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(0, qubits[2].measure());
}
示例15
@Test
public void ToffoliGate1() {
// |100> -> |100>
Program p = new Program(3,
new Step(new X(2)),
new Step(new Toffoli(2,1,0))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(1, qubits[2].measure());
}
示例16
@Test
public void ToffoliGate2() {
// |110> -> |111>
Program p = new Program(3,
new Step(new X(2),new X(1)),
new Step(new Toffoli(2,1,0)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(1, qubits[0].measure());
assertEquals(1, qubits[1].measure());
assertEquals(1, qubits[2].measure());
}
示例17
@Test
public void ToffoliGate3() {
// |111> -> |110>
Program p = new Program(3,
new Step(new X(2),new X(1),new X(0)),
new Step(new Toffoli(2,1,0))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(1, qubits[1].measure());
assertEquals(1, qubits[2].measure());
}
示例18
@Test
public void ToffoliGate4() {
// |001> -> |001>
Program p = new Program(3,
new Step(new X(0)),
new Step(new Toffoli(2,1,0))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(1, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(0, qubits[2].measure());
}
示例19
@Test
public void ToffoliGateR0() {
// |000> -> |000>
Program p = new Program(3, new Step(new Toffoli(0,1,2)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(0, qubits[2].measure());
}
示例20
@Test
public void ToffoliGateR1() {
// |100> -> |100>
Program p = new Program(3,
new Step(new X(2)),
new Step(new Toffoli(0,1,2))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(1, qubits[2].measure());
}
示例21
@Test
public void ToffoliGateR2() {
// |011> -> |111>
Program p = new Program(3,
new Step(new X(0),new X(1)),
new Step(new Toffoli(0, 1, 2)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(3, qubits.length);
assertEquals(1, qubits[0].measure());
assertEquals(1, qubits[1].measure());
assertEquals(1, qubits[2].measure());
}
示例22
@Test
public void ToffoliGateR3() {
// |110> -> |111>
Program p = new Program(4,
new Step(new X(2),new X(3)),
new Step(new Toffoli(3, 2, 1)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(4, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(1, qubits[1].measure());
assertEquals(1, qubits[2].measure());
assertEquals(1, qubits[3].measure());
}
示例23
@Test
public void ToffoliGateR4() {
System.err.println("R4");
// |1100> -> |1101>
Program p = new Program(4,
new Step(new X(2),new X(3)),
new Step(new Toffoli(3, 2, 0)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(4, qubits.length);
assertEquals(1, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(1, qubits[2].measure());
assertEquals(1, qubits[3].measure());
}
示例24
@Test
public void ToffoliGateS2() {
System.err.println("S2");
// |0101> -> |1101>
Program p = new Program(4,
new Step(new X(0),new X(2)),
new Step(new Toffoli(0, 2, 3)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(4, qubits.length);
assertEquals(1, qubits[0].measure());
assertEquals(0, qubits[1].measure());
assertEquals(1, qubits[2].measure());
assertEquals(1, qubits[3].measure());
}
示例25
@Test
public void hcnot01() {
Program p = new Program(2,
new Step(new Hadamard(0)),
new Step(new Cnot(0,1))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(2, qubits.length);
int q0 = qubits[0].measure();
int q1 = qubits[1].measure();
assertEquals(q0,q1);
}
示例26
@Test
public void simpleXM() {
Program p = new Program(1,
new Step(new X(0)),
new Step(new Measurement(0))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(1, qubits[0].measure());
}
示例27
/**
* BellState with a third qubit that is sent through a H gate
*/
@Test
public void cnotH() {
Program p = new Program(3,
new Step(new Hadamard(0)),
new Step(new Cnot(0,1)),
new Step(new Hadamard(2))
);
Result res = runProgram(p);
int zeroCount = 0;
int q2count0 = 0;
final int RUNS = 100;
for (int i = 0; i < RUNS; i++) {
res.measureSystem();
Qubit[] qubits = res.getQubits();
int q0 = qubits[0].measure();
int q1 = qubits[1].measure();
int q2 = qubits[2].measure();
assertEquals(q0,q1);
if (q0 == 0) zeroCount++;
if (q2 == 0) q2count0++;
}
assertTrue(zeroCount > 0);
assertTrue(zeroCount < RUNS);
assertTrue(q2count0 > 0);
assertTrue(q2count0 < RUNS);
}
示例28
@Test
public void swapGate00() {
Program p = new Program(2,
new Step(new Identity(0),new Identity(1)),
new Step(new Swap(0,1))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(2, qubits.length);
assertEquals(0, qubits[0].measure());
assertEquals(0, qubits[1].measure());
}
示例29
@Test
public void swapGate01() {
Program p = new Program(2,
new Step(new Identity(0),new X(1)),
new Step(new Swap(0,1))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(2, qubits.length);
assertEquals(1, qubits[0].measure());
assertEquals(0, qubits[1].measure());
}
示例30
@Test
public void swapGate10() {
Program p = new Program(2,
new Step(new Identity(1),new X(0)),
new Step(new Swap(0,1))
);
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
assertEquals(2, qubits.length);
assertEquals(1, qubits[1].measure());
assertEquals(0, qubits[0].measure());
}