Skip to content

Commit

Permalink
Replace dbg with java logging framework. (#144)
Browse files Browse the repository at this point in the history
Fix #141
  • Loading branch information
johanvos authored Dec 21, 2024
1 parent ac3689b commit f9b7033
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/redfx/strange/local/Computations.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Computations {
private static final boolean debug = false;

static void dbg(String s) {
SimpleQuantumExecutionEnvironment.dbg(s);
SimpleQuantumExecutionEnvironment.LOG.finer(s);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Logger;

/**
* <p>SimpleQuantumExecutionEnvironment class.</p>
Expand All @@ -50,17 +51,7 @@
*/
public class SimpleQuantumExecutionEnvironment implements QuantumExecutionEnvironment {

/**
* <p>dbg.</p>
*
* @param s a {@link java.lang.String} object
*/
public static void dbg (String s) {
String dbp = System.getProperty("dbg", "false");
if (dbp.equals("true")) {
System.err.println("[DBG] "+ s);
}
}
static Logger LOG = Logger.getLogger(SimpleQuantumExecutionEnvironment.class.getName());

/**
* <p>Constructor for SimpleQuantumExecutionEnvironment.</p>
Expand All @@ -71,7 +62,7 @@ public SimpleQuantumExecutionEnvironment() {
/** {@inheritDoc} */
@Override
public Result runProgram(Program p) {
dbg("runProgram ");
LOG.info("runProgram ");
int nQubits = p.getNumberQubits();
Qubit[] qubit = new Qubit[nQubits];
for (int i = 0; i < nQubits; i++) {
Expand Down Expand Up @@ -106,15 +97,15 @@ public Result runProgram(Program p) {
Result result = new Result(nQubits, steps.size());
int cnt = 0;
result.setIntermediateProbability(0, probs);
dbg("START RUN, number of steps = " + simpleSteps.size());
LOG.fine("START RUN, number of steps = " + simpleSteps.size());
for (Step step : simpleSteps) {
if (!step.getGates().isEmpty()) {
dbg("RUN STEP " + step + ", cnt = " + cnt);
LOG.finer("RUN STEP " + step + ", cnt = " + cnt);
cnt++;
dbg("before this step, probs = ");
LOG.finest("before this step, probs = ");
// printProbs(probs);
probs = applyStep(step, probs, qubit);
dbg("after this step, probs = "+probs);
LOG.finest("after this step, probs = "+probs);
// printProbs(probs);
int idx = step.getComplexStep();
// System.err.println("complex? "+idx);
Expand All @@ -123,7 +114,7 @@ public Result runProgram(Program p) {
}
}
}
dbg("DONE RUN, probability vector = " + probs);
LOG.info("DONE RUN, probability vector = " + probs);
printProbs(probs);
double[] qp = calculateQubitStatesFromVector(probs);
for (int i = 0; i < nQubits; i++) {
Expand Down Expand Up @@ -151,7 +142,7 @@ private List<Step> decomposeSteps(List<Step> steps) {
}

private Complex[] applyStep (Step step, Complex[] vector, Qubit[] qubits) {
dbg("start applystep, vectorsize = "+vector.length+", ql = "+qubits.length);
LOG.finer("start applystep, vectorsize = "+vector.length+", ql = "+qubits.length);
long s0 = System.currentTimeMillis();
List<Gate> gates = step.getGates();
if (!gates.isEmpty() && gates.get(0) instanceof ProbabilitiesGate ) {
Expand All @@ -169,16 +160,16 @@ private Complex[] applyStep (Step step, Complex[] vector, Qubit[] qubits) {
if (vdd) {
result = Computations.calculateNewState(gates, vector, qubits.length);
} else {
dbg("start calcstepmatrix with gates " + gates);
LOG.finest("start calcstepmatrix with gates " + gates);
Complex[][] a = calculateStepMatrix(gates, qubits.length);
dbg("done calcstepmatrix");
dbg("vector");
LOG.finest("done calcstepmatrix");
LOG.finest("vector");
// printProbs(vector);
if (a.length != result.length) {
System.err.println("fatal issue calculating step for gates " + gates);
throw new RuntimeException("Wrong length of matrix or probability vector: expected " + result.length + " but got " + a.length);
}
dbg("start matrix-vector multiplication for vector size = " + vector.length);
LOG.finest("start matrix-vector multiplication for vector size = " + vector.length);
for (int i = 0; i < vector.length; i++) {
result[i] = Complex.ZERO;
for (int j = 0; j < vector.length; j++) {
Expand All @@ -187,7 +178,7 @@ private Complex[] applyStep (Step step, Complex[] vector, Qubit[] qubits) {
}
}
long s1 = System.currentTimeMillis();
dbg("done applystep took "+ (s1-s0));
LOG.finer("done applystep took "+ (s1-s0));

return result;
}
Expand Down

0 comments on commit f9b7033

Please sign in to comment.