Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Controlled Hadamard Gate #138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/org/redfx/strange/gate/Chadamard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.redfx.strange.gate;

import org.redfx.strange.Complex;

public class Chadamard extends TwoQubitGate {
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.HC,Complex.HC},
{Complex.ZERO,Complex.ZERO,Complex.HC,Complex.HCN}
};

/**
* <p>Constructor for Chadamard.</p>
*/
public Chadamard() {
}

/**
* <p>Constructor for Chadamard.</p>
*
* @param a a int
* @param b a int
*/
public Chadamard(int a, int b) {
super(a,b);
}

/** {@inheritDoc} */
@Override
public Complex[][] getMatrix() {
return matrix;
}

/** {@inheritDoc} */
@Override public String getCaption() {
return "Chadamard";
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/redfx/strange/test/TwoQubitGateTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.redfx.strange.gate.Measurement;
import org.redfx.strange.gate.Swap;
import org.redfx.strange.gate.X;
import org.redfx.strange.gate.Hadamard;
import org.redfx.strange.gate.Chadamard;

/**
*
Expand Down Expand Up @@ -309,4 +311,49 @@ public void IMcnot10() {
IllegalArgumentException.class,
() -> p.addStep(new Step(new Cnot(1,0))));
}

@Test
public void chadamardGate() {
int[] results = new int[2];
for (int i = 0; i < 100; i++) {
Program p = new Program(2,
new Step(new Hadamard(0)),
new Step(new Chadamard(0,1)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
results[qubits[1].measure()]++;
}
assertTrue(results[0] > 10);
assertTrue(results[1] > 10);
}

@Test
public void chadamardXGate() {
int[] results = new int[2];
for (int i = 0; i < 100; i++) {
Program p = new Program(2,
new Step(new X(0)),
new Step(new Chadamard(0,1)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
results[qubits[1].measure()]++;
}
assertTrue(results[0] > 10);
assertTrue(results[1] > 10);
}

@Test
public void chadamardNotApplied() {
int[] results = new int[2];
for (int i = 0; i < 100; i++) {
Program p = new Program(2,
new Step(new Chadamard(0,1)));
Result res = runProgram(p);
Qubit[] qubits = res.getQubits();
results[qubits[1].measure()]++;
}
assertEquals(100, results[0]);
assertEquals(0, results[1]);
}

}