Skip to content

Commit

Permalink
Merge pull request #230 from quantumlib/unitary-space
Browse files Browse the repository at this point in the history
Fix unitary calculator.
  • Loading branch information
sergeisakov authored Nov 2, 2020
2 parents e283b8b + 586b499 commit 2d9a9cf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/unitary_calculator_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class UnitaryCalculatorBasic final {
using Unitary = typename UnitarySpace::Unitary;
using fp_type = typename UnitarySpace::fp_type;

using StateSpace = UnitarySpace;
using State = Unitary;

template <typename... ForArgs>
explicit UnitaryCalculatorBasic(unsigned num_qubits, ForArgs&&... args)
: for_(args...), num_qubits_(num_qubits) {}
Expand Down
3 changes: 3 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ cc_library(
}),
deps = [
"@com_google_googletest//:gtest_main",
"//lib:fuser",
"//lib:gate_appl",
"//lib:gates_cirq",
],
testonly = 1,
)
Expand Down
4 changes: 4 additions & 0 deletions tests/unitary_calculator_basic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ TEST(UnitaryCalculatorTest, ApplyGate2) {
TestApplyGate2<UnitaryCalculatorBasic<For, float>>();
}

TEST(UnitaryCalculatorTest, ApplyFusedGate) {
TestApplyFusedGate<UnitaryCalculatorBasic<For, float>>();
}

} // namspace
} // namespace unitary
} // namespace qsim
Expand Down
47 changes: 43 additions & 4 deletions tests/unitary_calculator_testfixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

#include "gtest/gtest.h"

#include "../lib/fuser.h"
#include "../lib/gate_appl.h"
#include "../lib/gates_cirq.h"

namespace qsim {

namespace unitary {
Expand All @@ -28,8 +32,8 @@ namespace {
template <typename UnitarySpace, typename Unitary>
void FillMatrix(UnitarySpace& us, Unitary& u, int n) {
// Intentionally create non-unitary matrix with ascending elements.
for(int i =0; i < (1 << n); i++){
for(int j =0;j < (1 << n); j++) {
for(int i = 0; i < (1 << n); i++) {
for(int j = 0; j < (1 << n); j++) {
us.SetEntry(u, i, j, 2 * i * (1 << n) + 2 * j,
2 * i * (1 << n) + 2 * j + 1);
}
Expand All @@ -38,8 +42,6 @@ void FillMatrix(UnitarySpace& us, Unitary& u, int n) {

} // namespace

constexpr char provider[] = "unitary_calculator_test";

template <typename UC>
void TestApplyGate1() {
const int n_qubits = 3;
Expand Down Expand Up @@ -222,6 +224,43 @@ void TestApplyGate2() {
}
}

template <typename UnitaryCalculator>
void TestApplyFusedGate() {
using UnitarySpace = typename UnitaryCalculator::UnitarySpace;
using Unitary = typename UnitaryCalculator::Unitary;
using fp_type = typename UnitaryCalculator::fp_type;
using Gate = Cirq::GateCirq<fp_type>;

unsigned num_qubits = 1;

UnitaryCalculator uc(num_qubits, 1);
UnitarySpace us(num_qubits, 1);

Unitary u = us.CreateUnitary();
us.SetIdentity(u);

std::vector<Gate> gates = {Cirq::H<fp_type>::Create(0, 0),
Cirq::H<fp_type>::Create(1, 0)};

GateFused<Gate> fgate{Cirq::kH, 0, {0}, &gates[0], {&gates[0], &gates[1]}};

ApplyFusedGate(uc, fgate, u);

unsigned size = 1 << num_qubits;
for (unsigned i = 0; i < size; ++i) {
for (unsigned j = 0; j < size; ++j) {
auto a = us.GetEntry(u, i, j);
if (i == j) {
EXPECT_NEAR(std::real(a), 1, 1e-6);
} else {
EXPECT_NEAR(std::real(a), 0, 1e-6);
}

EXPECT_NEAR(std::imag(a), 0, 1e-6);
}
}
}

} // namespace unitary
} // namespace qsim

Expand Down

0 comments on commit 2d9a9cf

Please sign in to comment.