From 2c7e8cf9be5a462b3381faa23f19bf2715aa6c80 Mon Sep 17 00:00:00 2001 From: Jae Yoo Date: Thu, 30 Sep 2021 18:12:41 +0000 Subject: [PATCH 1/3] Add fp_type and ApplyFusedGate test for MPSSimulator --- .vscode/settings.json | 70 +++++++++++++++++++++++++++++++++++++ lib/mps_simulator.h | 5 +-- lib/mps_statespace.h | 3 +- tests/mps_simulator_test.cc | 48 +++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..dc06896c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,70 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "hash_map": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "ranges": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp", + "valarray": "cpp" + } +} \ No newline at end of file diff --git a/lib/mps_simulator.h b/lib/mps_simulator.h index ae053690..98d00310 100644 --- a/lib/mps_simulator.h +++ b/lib/mps_simulator.h @@ -35,11 +35,12 @@ namespace mps { /** * Truncated Matrix Product State (MPS) circuit simulator w/ vectorization. */ -template +template class MPSSimulator final { public: - using MPSStateSpace_ = MPSStateSpace; + using MPSStateSpace_ = MPSStateSpace; using State = typename MPSStateSpace_::MPS; + using fp_type = typename MPSStateSpace_::fp_type; using Complex = std::complex; using Matrix = diff --git a/lib/mps_statespace.h b/lib/mps_statespace.h index 0520ef9f..26cfe190 100644 --- a/lib/mps_statespace.h +++ b/lib/mps_statespace.h @@ -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 +template class MPSStateSpace { private: public: + using fp_type = FP; using Pointer = std::unique_ptr; using Complex = std::complex; diff --git a/tests/mps_simulator_test.cc b/tests/mps_simulator_test.cc index 98092fd3..f70e63de 100644 --- a/tests/mps_simulator_test.cc +++ b/tests/mps_simulator_test.cc @@ -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 { @@ -265,6 +266,53 @@ TEST(MPSSimulator, Apply1InteriorArbitrary) { ASSERT_NEAR(state.get()[l_offset + 63], 153.6, 1e-5); } +TEST(MPSSimulator, ApplyLeftFusedGate) { + // Apply a fused gate matrix to the first two qubits. + // Compute the state vector of: + // | | | + // +-+-----+-+ | + // |FusedGate| | + // +-+-----+-+ | + // | | | + // +-+-+ +-+-+ +-+-+ + // | 0 +-+ 1 +-+ 2 | + // +---+ +---+ +---+ + auto sim = MPSSimulator(1); + using MPSStateSpace = MPSSimulator::MPSStateSpace_; + auto ss = MPSStateSpace(1); + + auto gate1 = GateCZ::Create(2, 0, 1); + auto gate2 = GateHd::Create(0, 0); + auto gate3 = GateHd::Create(0, 1); + + GateFused> fgate1{kGateCZ, 2, {0, 1}, &gate1, {&gate2, &gate3}}; + auto mps = ss.CreateMPS(3, 2); + ss.SetMPSZero(mps); + ApplyFusedGate(sim, fgate1, mps); + + // float wf[32]; + // ss.ToWaveFunction(mps, wf); + // for (int i = 0; i < 16; i++) { + // std::cerr << wf[i] << std::endl; + // } + // EXPECT_NEAR(wf[0], 100, 1e-4); + // EXPECT_NEAR(wf[1], 0.2143698948599676, 1e-4); + // EXPECT_NEAR(wf[2], -0.11738977370009586, 1e-4); + // EXPECT_NEAR(wf[3], 0.12454935773362025, 1e-4); + // EXPECT_NEAR(wf[4], -0.47150921436100324, 1e-4); + // EXPECT_NEAR(wf[5], 0.2765580906536361, 1e-4); + // EXPECT_NEAR(wf[6], -0.00488996377601076, 1e-4); + // EXPECT_NEAR(wf[7], -0.052702222730185794, 1e-4); + // EXPECT_NEAR(wf[8], -0.15375504635790666, 1e-4); + // EXPECT_NEAR(wf[9], -0.006893208509005258, 1e-4); + // EXPECT_NEAR(wf[10], 0.3625325032891465, 1e-4); + // EXPECT_NEAR(wf[11], -0.12932957404571366, 1e-4); + // EXPECT_NEAR(wf[12], -0.3459372670125525, 1e-4); + // EXPECT_NEAR(wf[13], 0.4020253863039828, 1e-4); + // EXPECT_NEAR(wf[14], -0.21864624250067458, 1e-4); + // EXPECT_NEAR(wf[15], -0.16468189647310463, 1e-4); +} + TEST(MPSSimulator, Apply2Left01) { // Compute the state vector of: // | | | From 089101cbdb372c61fc7d557b278a355e4e2d5c2c Mon Sep 17 00:00:00 2001 From: Jae Yoo Date: Thu, 30 Sep 2021 19:09:02 +0000 Subject: [PATCH 2/3] Add MPSSimulator ApplyFusedGate{L,R,M} tests --- tests/mps_simulator_test.cc | 151 +++++++++++++++++++++++++----------- 1 file changed, 104 insertions(+), 47 deletions(-) diff --git a/tests/mps_simulator_test.cc b/tests/mps_simulator_test.cc index 589a627f..605a29c0 100644 --- a/tests/mps_simulator_test.cc +++ b/tests/mps_simulator_test.cc @@ -266,53 +266,6 @@ TEST(MPSSimulator, Apply1InteriorArbitrary) { ASSERT_NEAR(state.get()[l_offset + 63], 153.6, 1e-5); } -TEST(MPSSimulator, ApplyLeftFusedGate) { - // Apply a fused gate matrix to the first two qubits. - // Compute the state vector of: - // | | | - // +-+-----+-+ | - // |FusedGate| | - // +-+-----+-+ | - // | | | - // +-+-+ +-+-+ +-+-+ - // | 0 +-+ 1 +-+ 2 | - // +---+ +---+ +---+ - auto sim = MPSSimulator(1); - using MPSStateSpace = MPSSimulator::MPSStateSpace_; - auto ss = MPSStateSpace(1); - - auto gate1 = GateCZ::Create(2, 0, 1); - auto gate2 = GateHd::Create(0, 0); - auto gate3 = GateHd::Create(0, 1); - - GateFused> fgate1{kGateCZ, 2, {0, 1}, &gate1, {&gate2, &gate3}}; - auto mps = ss.CreateMPS(3, 2); - ss.SetMPSZero(mps); - ApplyFusedGate(sim, fgate1, mps); - - // float wf[32]; - // ss.ToWaveFunction(mps, wf); - // for (int i = 0; i < 16; i++) { - // std::cerr << wf[i] << std::endl; - // } - // EXPECT_NEAR(wf[0], 100, 1e-4); - // EXPECT_NEAR(wf[1], 0.2143698948599676, 1e-4); - // EXPECT_NEAR(wf[2], -0.11738977370009586, 1e-4); - // EXPECT_NEAR(wf[3], 0.12454935773362025, 1e-4); - // EXPECT_NEAR(wf[4], -0.47150921436100324, 1e-4); - // EXPECT_NEAR(wf[5], 0.2765580906536361, 1e-4); - // EXPECT_NEAR(wf[6], -0.00488996377601076, 1e-4); - // EXPECT_NEAR(wf[7], -0.052702222730185794, 1e-4); - // EXPECT_NEAR(wf[8], -0.15375504635790666, 1e-4); - // EXPECT_NEAR(wf[9], -0.006893208509005258, 1e-4); - // EXPECT_NEAR(wf[10], 0.3625325032891465, 1e-4); - // EXPECT_NEAR(wf[11], -0.12932957404571366, 1e-4); - // EXPECT_NEAR(wf[12], -0.3459372670125525, 1e-4); - // EXPECT_NEAR(wf[13], 0.4020253863039828, 1e-4); - // EXPECT_NEAR(wf[14], -0.21864624250067458, 1e-4); - // EXPECT_NEAR(wf[15], -0.16468189647310463, 1e-4); -} - TEST(MPSSimulator, Apply2Left01) { // Compute the state vector of: // | | | @@ -850,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(1); + using MPSStateSpace = MPSSimulator::MPSStateSpace_; + auto ss = MPSStateSpace(1); + + auto gate1 = GateCZ::Create(2, 0, 1); + auto gate2 = GateHd::Create(0, 0); + auto gate3 = GateHd::Create(0, 1); + + GateFused> 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(1); + using MPSStateSpace = MPSSimulator::MPSStateSpace_; + auto ss = MPSStateSpace(1); + + auto gate1 = GateCZ::Create(2, 1, 2); + auto gate2 = GateHd::Create(0, 1); + auto gate3 = GateHd::Create(0, 2); + + GateFused> 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(1); + using MPSStateSpace = MPSSimulator::MPSStateSpace_; + auto ss = MPSStateSpace(1); + + auto gate1 = GateCZ::Create(2, 1, 2); + auto gate2 = GateHd::Create(0, 1); + auto gate3 = GateHd::Create(0, 2); + + GateFused> 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 From 016af85eeef472f4dc8f69a4e410584e2d429e68 Mon Sep 17 00:00:00 2001 From: Jae Yoo Date: Thu, 30 Sep 2021 19:17:45 +0000 Subject: [PATCH 3/3] Fix BUILD and remove .vscode/settings.json --- .vscode/settings.json | 70 ------------------------------------------- tests/BUILD | 1 + 2 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index dc06896c..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,70 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "forward_list": "cpp", - "list": "cpp", - "map": "cpp", - "set": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "string": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "hash_map": "cpp", - "fstream": "cpp", - "future": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "ostream": "cpp", - "ranges": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "cinttypes": "cpp", - "typeinfo": "cpp", - "valarray": "cpp" - } -} \ No newline at end of file diff --git a/tests/BUILD b/tests/BUILD index 53f72ab4..47f90535 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -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", ],