Skip to content

Commit

Permalink
Merge pull request #437 from jaeyoo/add_fp_type_in_mps_sim
Browse files Browse the repository at this point in the history
Support ApplyFusedGate in MPSSimulator
  • Loading branch information
MichaelBroughton authored Sep 30, 2021
2 parents 96a85d8 + 016af85 commit da4cc61
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/mps_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ namespace mps {
/**
* Truncated Matrix Product State (MPS) circuit simulator w/ vectorization.
*/
template <typename For, typename fp_type = float>
template <typename For, typename FP = float>
class MPSSimulator final {
public:
using MPSStateSpace_ = MPSStateSpace<For, fp_type>;
using MPSStateSpace_ = MPSStateSpace<For, FP>;
using State = typename MPSStateSpace_::MPS;
using fp_type = typename MPSStateSpace_::fp_type;

using Complex = std::complex<fp_type>;
using Matrix =
Expand Down
3 changes: 2 additions & 1 deletion lib/mps_statespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ inline void free(void* ptr) {
* Class containing context and routines for fixed bond dimension
* truncated Matrix Product State (MPS) simulation.
*/
template <typename For, typename fp_type = float>
template <typename For, typename FP = float>
class MPSStateSpace {
private:
public:
using fp_type = FP;
using Pointer = std::unique_ptr<fp_type, decltype(&detail::free)>;

using Complex = std::complex<fp_type>;
Expand Down
1 change: 1 addition & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ cc_test(
"@com_google_googletest//:gtest_main",
"//lib:gate_appl",
"//lib:gates_cirq",
"//lib:gates_qsim",
"//lib:mps_simulator",
"//lib:formux",
],
Expand Down
105 changes: 105 additions & 0 deletions tests/mps_simulator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../lib/formux.h"
#include "../lib/gate_appl.h"
#include "../lib/gates_cirq.h"
#include "../lib/gates_qsim.h"
#include "gtest/gtest.h"

namespace qsim {
Expand Down Expand Up @@ -802,6 +803,110 @@ TEST(MPSSimulator, OneTwoQubitFuzz) {
*/
}

TEST(MPSSimulator, ApplyFusedGateLeft) {
// Apply a fused gate matrix to the first two qubits.
// Compute the state vector of:
// | | |
// +-+-----+-+ |
// |FusedGate| |
// +-+-----+-+ |
// | | |
// +-+-+ +-+-+ +-+-+
// | 0 +-+ 1 +-+ 2 |
// +---+ +---+ +---+
auto sim = MPSSimulator<For, float>(1);
using MPSStateSpace = MPSSimulator<For, float>::MPSStateSpace_;
auto ss = MPSStateSpace(1);

auto gate1 = GateCZ<float>::Create(2, 0, 1);
auto gate2 = GateHd<float>::Create(0, 0);
auto gate3 = GateHd<float>::Create(0, 1);

GateFused<GateQSim<float>> fgate1{kGateCZ, 2, {0, 1}, &gate1,
{&gate2, &gate3}};
auto mps = ss.Create(3, 4);
ss.SetStateZero(mps);
ApplyFusedGate(sim, fgate1, mps);

float wf[32];
float ground_truth[] = {0.5, 0., 0., 0., 0.5, 0., 0., 0.,
0.5, 0., 0., 0., 0.5, 0., 0., 0.};
ss.ToWaveFunction(mps, wf);
for (int i = 0; i < 16; i++) {
EXPECT_NEAR(wf[i], ground_truth[i], 1e-4);
}
}

TEST(MPSSimulator, ApplyFusedGateRight) {
// Apply a fused gate matrix to the last two qubits.
// Compute the state vector of:
// | | |
// | +-+-----+-+
// | |FusedGate|
// | +-+-----+-+
// | | |
// +-+-+ +-+-+ +-+-+
// | 0 +-+ 1 +-+ 2 |
// +---+ +---+ +---+
auto sim = MPSSimulator<For, float>(1);
using MPSStateSpace = MPSSimulator<For, float>::MPSStateSpace_;
auto ss = MPSStateSpace(1);

auto gate1 = GateCZ<float>::Create(2, 1, 2);
auto gate2 = GateHd<float>::Create(0, 1);
auto gate3 = GateHd<float>::Create(0, 2);

GateFused<GateQSim<float>> fgate1{kGateCZ, 2, {1, 2}, &gate1,
{&gate2, &gate3}};
auto mps = ss.Create(3, 4);
ss.SetStateZero(mps);
ApplyFusedGate(sim, fgate1, mps);

float wf[32];
float ground_truth[] = {0.5, 0., 0.5, 0., 0.5, 0., 0.5, 0.,
0., 0., 0., 0., 0., 0., 0., 0.};
ss.ToWaveFunction(mps, wf);
for (int i = 0; i < 16; i++) {
EXPECT_NEAR(wf[i], ground_truth[i], 1e-4);
}
}

TEST(MPSSimulator, ApplyFusedGateMiddle) {
// Apply a fused gate matrix to the middle two qubits.
// Compute the state vector of:
// | | | |
// | +-+-----+-+ |
// | |FusedGate| |
// | +-+-----+-+ |
// | | | |
// +-+-+ +-+-+ +-+-+ +-+-+
// | 0 +-+ 1 +-+ 2 |-| 3 |
// +---+ +---+ +---+ +-+-+
auto sim = MPSSimulator<For, float>(1);
using MPSStateSpace = MPSSimulator<For, float>::MPSStateSpace_;
auto ss = MPSStateSpace(1);

auto gate1 = GateCZ<float>::Create(2, 1, 2);
auto gate2 = GateHd<float>::Create(0, 1);
auto gate3 = GateHd<float>::Create(0, 2);

GateFused<GateQSim<float>> fgate1{kGateCZ, 2, {1, 2}, &gate1,
{&gate2, &gate3}};
auto mps = ss.Create(4, 4);
ss.SetStateZero(mps);
ApplyFusedGate(sim, fgate1, mps);

float wf[64];
float ground_truth[] = {0.5, 0., 0., 0., 0.5, 0., 0., 0.,
0.5, 0., 0., 0., 0.5, 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0.};
ss.ToWaveFunction(mps, wf);
for (int i = 0; i < 32; i++) {
EXPECT_NEAR(wf[i], ground_truth[i], 1e-4);
}
}

} // namespace
} // namespace mps
} // namespace qsim
Expand Down

0 comments on commit da4cc61

Please sign in to comment.