Commit 00160f79 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added tests for grouping


Signed-off-by: Nguyen, Thien Minh's avatarThien Nguyen <nguyentm@ornl.gov>
parent 62f67156
...@@ -277,7 +277,7 @@ PauliOperator::observe(std::shared_ptr<CompositeInstruction> function, const Het ...@@ -277,7 +277,7 @@ PauliOperator::observe(std::shared_ptr<CompositeInstruction> function, const Het
} }
// For this, we require the Accelerator info: // For this, we require the Accelerator info:
if (grouping_options.pointerLikeExists<Accelerator>("accelerator")) { if (!grouping_options.pointerLikeExists<Accelerator>("accelerator")) {
xacc::error("'accelerator' is required for Observable grouping"); xacc::error("'accelerator' is required for Observable grouping");
} }
auto qpu = grouping_options.getPointerLike<Accelerator>("accelerator"); auto qpu = grouping_options.getPointerLike<Accelerator>("accelerator");
......
...@@ -490,6 +490,29 @@ TEST(PauliOperatorTester, checkNormalize) { ...@@ -490,6 +490,29 @@ TEST(PauliOperatorTester, checkNormalize) {
} }
TEST(PauliOperatorTester, checkGroupingQaoa) {
PauliOperator op;
op.fromString("(0, -1) Z0 Z1 + (0, -1) Z1 Z2 + (0, -1) Z2 Z0");
std::cout << op.toString() << "\n";
auto qpu = xacc::getAccelerator("qpp", {{"shots", 1024}});
auto gateRegistry = xacc::getService<xacc::IRProvider>("quantum");
auto f = gateRegistry->createComposite("f");
auto h0 = gateRegistry->createInstruction("H", 0);
auto h1 = gateRegistry->createInstruction("H", 1);
auto h2 = gateRegistry->createInstruction("H", 2);
f->addInstructions({h0, h1, h2});
auto observed = op.observe(f, {{"accelerator", qpu}});
EXPECT_EQ(observed.size(), 1);
// Check non-commute as well:
PauliOperator op_non_commute;
op_non_commute.fromString("(0, -1) Z0 Z1 + (0, -1) Z1 Z2 + (0, -1) Z2 X0");
std::cout << op_non_commute.toString() << "\n";
auto observed_non_commute = op_non_commute.observe(f, {{"accelerator", qpu}});
// Three terms:
EXPECT_EQ(observed_non_commute.size(), 3);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
xacc::Initialize(argc, argv); xacc::Initialize(argc, argv);
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
......
...@@ -28,6 +28,7 @@ public: ...@@ -28,6 +28,7 @@ public:
virtual void initialize(const HeterogeneousMap& params = {}) override; virtual void initialize(const HeterogeneousMap& params = {}) override;
virtual void updateConfiguration(const HeterogeneousMap& config) override {initialize(config);}; virtual void updateConfiguration(const HeterogeneousMap& config) override {initialize(config);};
virtual const std::vector<std::string> configurationKeys() override { return {}; } virtual const std::vector<std::string> configurationKeys() override { return {}; }
virtual HeterogeneousMap getProperties() override { return {{"shots", m_shots}}; }
virtual BitOrder getBitOrder() override {return BitOrder::LSB;} virtual BitOrder getBitOrder() override {return BitOrder::LSB;}
virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer, const std::shared_ptr<CompositeInstruction> compositeInstruction) override; virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer, const std::shared_ptr<CompositeInstruction> compositeInstruction) override;
virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer, const std::vector<std::shared_ptr<CompositeInstruction>> compositeInstructions) override; virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer, const std::vector<std::shared_ptr<CompositeInstruction>> compositeInstructions) override;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment