Commit 4de3a61b authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Merge branch 'master' into tnguyen/update-qasm3

parents 5ee18f03 207fd011
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -5,10 +5,13 @@ __qpu__ void h_gate(qreg q) { H(q[0]); }

int main() {
  int n_state_qubits = 1;
  auto expectation = qcor::hadamard_test(x_gate, x_gate, n_state_qubits);
  print("<X> = ", expectation);
  auto expectation = qcor::hadamard_test(h_gate, x_gate, n_state_qubits);
  print("< + | X | + > = ", expectation);
  
  expectation = qcor::hadamard_test(x_gate, h_gate, n_state_qubits);
  print("<H> = ", expectation);
  print("< 1 | H | 1 > = ", expectation);

  expectation = qcor::hadamard_test(x_gate, x_gate, n_state_qubits);
  print("< 1 | X | 1 > = ", expectation);
  return 0;
}
 No newline at end of file
+21 −10
Original line number Diff line number Diff line
OPENQASM 3;

// compute < psi | U | psi> 
// let |psi> = |+>, U = X
// < + | X | + > == 1.0
// == < 0 | H X H | 0> == < 0 | Z | 0 > == 1

const n_iters = 100;
double count0, count1;
// compile and run with 
// qcor hadamard_test.qasm 
// ./a.out

// Eigenstate qubit
qubit q;
const n_iters = 100;
double count1;

// Ancilla qubit
qubit a;

// Eigenstate qubit
qubit q;

for i in [0:n_iters] {
    // Generate |+> eigenstate
    x q;
    h q;

    // apply hadamard on ancilla
    h a;

    // Ctrl-U, U == H
    // Ctrl-U, U == X
    cx a, q;

    // apply hadamard again
    h a;

    // measure and reset
    // measure the ancilla
    bit c;
    c = measure a;

    // Reset the qubits
    reset a;
    reset q;

    // Store up the observed bits
    if (c == 1) {
@@ -36,7 +46,8 @@ for i in [0:n_iters] {
}

// Number of 0s must be ...
count0 = n_iters - count1;
double count0 = n_iters - count1;

double exp_val = (count1 - count0) / (count1 + count0);
print("<+|H|+> = ", exp_val);
 No newline at end of file
// compute the exp val Re(<psi|U|psi>) = P0 - P1
double exp_val = (count0 - count1) / n_iters;
print("<+|X|+> = ", exp_val);
 No newline at end of file
+2 −1
Original line number Diff line number Diff line
@@ -42,8 +42,9 @@ double hadamard_test(StatePrep state_prep, Unitary unitary,
  }
 
  // We have counts, so use that
  // P0 - P1 = <psi|U|psi>
  double count1 = (double)q.counts().find("1")->second;
  double count2 = (double)q.counts().find("0")->second;
  return std::fabs((count1 - count2) / (count1 + count2));
  return (count2 - count1) / (count1 + count2);
}
}  // namespace qcor