Skip to content
Merged
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
6 changes: 5 additions & 1 deletion src/loop_summarization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,11 @@ Symbol eval_symbol( const AUTOQ::Symbol::Symbolic& symbol,
for(auto& [term, complex] : sym.complex){
for(auto& [string, cppint] : term){
if(string.back() == 'I'){
mapping[term] = conc.complex.imag();
// A variable v is encoded as vR + i·vI, so vI must be
// bound to the real number Im(x). imag() returns
// i·Im(x); rotating it by a quarter turn clockwise
// (multiplying by -i) gives Im(x).
mapping[term] = conc.complex.imag().clockwise(boost::rational<boost::multiprecision::cpp_int>(1, 4));
}
else if(string.back() == 'R'){
mapping[term] = conc.complex.real();
Expand Down
34 changes: 34 additions & 0 deletions unit_tests/explicit_tree_aut_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <filesystem>
#include <fstream>
#include <functional>
#include <sstream>

#include "autoq/error.hh"
#include "autoq/inclusion.hh"
Expand Down Expand Up @@ -290,6 +291,39 @@ BOOST_AUTO_TEST_CASE(for_loop_summarization){
BOOST_REQUIRE_MESSAGE(verify, folder + " failed!");
}

// Runs the circuit on the precondition and returns the printed set of states.
static std::string execute_and_print(const std::string& pre, const std::string& circuit, const std::string& loop) {
auto [autVec, qp] = AUTOQ::Parsing::TimbukParser<AUTOQ::Symbol::Concrete>::ReadTwoAutomata(pre, pre, circuit);
auto aut = autVec.at(0);
autVec.erase(autVec.begin(), autVec.begin() + 2);
ParameterMap params;
params["loop"] = loop;
aut.execute(circuit, qp, autVec, params);
std::ostringstream out;
auto* old = std::cout.rdbuf(out.rdbuf());
aut.print_language();
std::cout.rdbuf(old);
return out.str();
}

// Loop summarization must give exactly the states that unrolling gives, also
// when an iteration receives an amplitude with a nonzero imaginary part (#22).
BOOST_AUTO_TEST_CASE(for_loop_summarization_complex_amplitudes){
std::string folder = test_dir_from_file(__FILE__, "testcase/ForLoopComplex/");
const std::vector<std::pair<std::string, std::string>> cases = {
{"complex_pre.hsl", "h_once.qasm"},
{"zero_pre.hsl", "ht_three_times.qasm"},
{"one_pre.hsl", "s_twice.qasm"},
{"two_qubit_pre.hsl", "two_qubit.qasm"},
};
for (const auto& [pre, circuit] : cases) {
auto unrolled = execute_and_print(folder + pre, folder + circuit, "manual");
auto summarized = execute_and_print(folder + pre, folder + circuit, "symbolic");
BOOST_CHECK_MESSAGE(unrolled == summarized,
pre + " + " + circuit + ": unrolled " + unrolled + " but summarized " + summarized);
}
}


static void grover_oracle(AUTOQ::ConcreteAutomata& aut, unsigned ans, int n) {
for (int i=1; i<=n; i++) {
Expand Down
2 changes: 2 additions & 0 deletions unit_tests/testcase/ForLoopComplex/complex_pre.hsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extended Dirac
{1/sqrt2 |0> + ei2pi(1/4)/sqrt2 |1>}
7 changes: 7 additions & 0 deletions unit_tests/testcase/ForLoopComplex/h_once.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OPENQASM 3;
include "stdgates.inc";
qubit[1] q;

for int i in [1:1] {
h q[0];
}
8 changes: 8 additions & 0 deletions unit_tests/testcase/ForLoopComplex/ht_three_times.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
OPENQASM 3;
include "stdgates.inc";
qubit[1] q;

for int i in [1:3] {
h q[0];
t q[0];
}
2 changes: 2 additions & 0 deletions unit_tests/testcase/ForLoopComplex/one_pre.hsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extended Dirac
{|1>}
7 changes: 7 additions & 0 deletions unit_tests/testcase/ForLoopComplex/s_twice.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OPENQASM 3;
include "stdgates.inc";
qubit[1] q;

for int i in [1:2] {
s q[0];
}
10 changes: 10 additions & 0 deletions unit_tests/testcase/ForLoopComplex/two_qubit.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
OPENQASM 3;
include "stdgates.inc";
qubit[2] q;

for int i in [1:3] {
y q[0];
h q[1];
cz q[0], q[1];
tdg q[0];
}
2 changes: 2 additions & 0 deletions unit_tests/testcase/ForLoopComplex/two_qubit_pre.hsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extended Dirac
{1/sqrt2 |00> + ei2pi(1/8)/sqrt2 |11>}
2 changes: 2 additions & 0 deletions unit_tests/testcase/ForLoopComplex/zero_pre.hsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extended Dirac
{|0>}
Loading