Adding initial testing support

This commit is contained in:
Juan Gomez 2018-08-09 16:39:19 +02:00
parent 661fc515bc
commit 02d2fe6714
9 changed files with 13551 additions and 4 deletions

4
.gitignore vendored
View File

@ -1,6 +1,6 @@
# Ignore editor settings # Ignore editor settings
.vscode/settings.json .vscode
# ignore jupyter notebook checkpoints # ignore jupyter notebook checkpoints
@ -13,6 +13,4 @@ out/*
*.so *.so
*.pyc *.pyc
src/simulators/qubitvector/qv_wrapper.cpp src/simulators/qubitvector/qv_wrapper.cpp
# Ignore build files
build/* build/*

View File

@ -3,7 +3,14 @@ WARNINGS = -pedantic -Wall -Wextra -Wfloat-equal -Wundef -Wcast-align -Wwrite-st
OPT = -O3 -march=native -ffast-math OPT = -O3 -march=native -ffast-math
OUTPUT_DIR = ../out/aer-simulator OUTPUT_DIR = ../out/aer-simulator
CC = g++ TEST_INCLUDE = -I../test/third-party
TEST_SRC_DIR = ../test/src
TEST_SOURCES := $(wildcard $(TEST_SRC_DIR)/test_*.cpp)
TEST_OUTPUT_DIR = $(OUTPUT_DIR)/test
TEST_EXE_NAMES := $(patsubst $(TEST_SRC_DIR)/test_%.cpp, $(TEST_OUTPUT_DIR)/test_%, $(TEST_SOURCES))
CC = ccache g++
# -march not supported on ppc64le (Power). Use -mcpu instead # -march not supported on ppc64le (Power). Use -mcpu instead
ifeq ($(shell uname -m),ppc64le) ifeq ($(shell uname -m),ppc64le)
@ -70,8 +77,23 @@ all: directories sim
directories: directories:
mkdir -p $(OUTPUT_DIR) mkdir -p $(OUTPUT_DIR)
test_directories:
mkdir -p $(TEST_OUTPUT_DIR)
sim: aer_simulator.o sim: aer_simulator.o
$(CC) $(CPPFLAGS) $(DEFINES) -o ${OUTPUT_DIR}/aer_simulator ${OUTPUT_DIR}/aer_simulator.o $(LIBS) $(CC) $(CPPFLAGS) $(DEFINES) -o ${OUTPUT_DIR}/aer_simulator ${OUTPUT_DIR}/aer_simulator.o $(LIBS)
clean: clean:
rm -rf ${OUTPUT_DIR} rm -rf ${OUTPUT_DIR}
test: test_directories $(TEST_EXE_NAMES)
$(TEST_OUTPUT_DIR)/test_%: $(TEST_SRC_DIR)/test_%.cpp
$(CC) -ggdb $(CPPFLAGS) $(TEST_INCLUDE) $(DEFINES) $< -o $@ $(LIBS)

View File

@ -0,0 +1,40 @@
{
"id": "matrix_observable_snapshot_example",
"type": "QASM",
"experiments": [
{
"config": {"shots": 1000},
"instructions": [
{"name": "h", "qubits": [0]},
{"name": "cx", "qubits": [0, 1]},
{"name": "snapshot", "type": "matrix_observable",
"label": "<ZZ>pre_measure",
"params": [{"coeff": 1, "qubits": [[1], [0]], "ops": [
[[[1, 0], [0, 0]], [[0, 0], [0, -1]]],
[[[1, 0], [0, 0]], [[1, 0], [0, -1]]]
]}]},
{"name": "snapshot", "type": "matrix_observable",
"label": "<ZI+IZ>pre_measure",
"params": [{"coeff": 1, "qubits": [[1]], "ops": [
[[[1, 0], [0, 0]], [[0, 0], [0, -1]]]]},
{"coeff": 1, "qubits": [[0]], "ops": [
[[[1, 0], [0, 0]], [[0, 0], [0, -1]]]]}
]},
{"name": "measure", "qubits": [0, 1], "memory": [0, 1]},
{"name": "snapshot", "type": "matrix_observable",
"label": "<ZZ>post_measure",
"params": [{"coeff": 1, "qubits": [[1], [0]], "ops": [
[[[1, 0], [0, 0]], [[0, 0], [0, -1]]],
[[[1, 0], [0, 0]], [[1, 0], [0, -1]]]
]}]},
{"name": "snapshot", "type": "matrix_observable",
"label": "<ZI+IZ>post_measure",
"params": [{"coeff": 1, "qubits": [[1]], "ops": [
[[[1, 0], [0, 0]], [[0, 0], [0, -1]]]]},
{"coeff": 1, "qubits": [[0]], "ops": [
[[[1, 0], [0, 0]], [[0, 0], [0, -1]]]]}
]}
]
}
]
}

View File

@ -0,0 +1,28 @@
{
"id": "pauli_observable_snapshot_example",
"type": "QASM",
"experiments": [
{
"config": {"shots": 1000},
"instructions": [
{"name": "h", "qubits": [0]},
{"name": "cx", "qubits": [0, 1]},
{"name": "snapshot", "type": "pauli_observable",
"label": "<ZZ>pre_measure",
"params": [{"coeff": 1, "qubits": [1, 0], "op": "ZZ"}]},
{"name": "snapshot", "type": "pauli_observable",
"label": "<ZI+IZ>pre_measure",
"params": [{"coeff": 1, "qubits": [1, 0], "op": "ZI"},
{"coeff": 1, "qubits": [1, 0], "op": "IZ"}]},
{"name": "measure", "qubits": [0, 1], "memory": [0, 1]},
{"name": "snapshot", "type": "pauli_observable",
"label": "<ZZ>post_measure",
"params": [{"coeff": 1, "qubits": [1, 0], "op": "ZZ"}]},
{"name": "snapshot", "type": "pauli_observable",
"label": "<ZI+IZ>post_measure",
"params": [{"coeff": 1, "qubits": [1, 0], "op": "ZI"},
{"coeff": 1, "qubits": [1, 0], "op": "IZ"}]}
]
}
]
}

View File

@ -0,0 +1,18 @@
{
"id": "probabilities_snapshot_example",
"type": "QASM",
"experiments": [
{
"config": {"shots": 1000},
"instructions": [
{"name": "h", "qubits": [0]},
{"name": "cx", "qubits": [0, 1]},
{"name": "snapshot", "type": "probabilities",
"label": "pre_measure", "qubits": [1, 0]},
{"name": "measure", "qubits": [0, 1], "memory": [0, 1]},
{"name": "snapshot", "type": "probabilities",
"label": "post_measure", "qubits": [1, 0]}
]
}
]
}

View File

@ -0,0 +1,16 @@
{
"id": "state_snapshot_example",
"type": "QASM",
"experiments": [
{
"config": {"shots": 1},
"instructions": [
{"name": "snapshot", "type": "state", "label": "initial"},
{"name": "h", "qubits": [0]},
{"name": "snapshot", "type": "state", "label": "middle"},
{"name": "cx", "qubits": [0, 1]},
{"name": "snapshot", "type": "state", "label": "final"}
]
}
]
}

View File

@ -0,0 +1,55 @@
#define CATCH_CONFIG_MAIN
#include <map>
#include <catch.hpp>
#include "base/controller.hpp"
#include "base/engine.hpp"
#include "simulators/qubitvector/qubitvector.hpp"
#include "simulators/qubitvector/qv_state.hpp"
#include "utils.hpp"
namespace AER::Test {
SCENARIO("We can get snapshots from different simulator types") {
GIVEN("A Qobj with snapshot information for every simulator type"){
std::map<std::string, json_t> qobj_snapshots;
qobj_snapshots["state"] =
AER::Test::Utilities::load_qobj("../../../test/data/qobj_snapshot_state.json");
qobj_snapshots["probs"] =
AER::Test::Utilities::load_qobj("../../../test/data/qobj_snapshot_probs.json");
qobj_snapshots["pauli"] =
AER::Test::Utilities::load_qobj("../../../test/data/qobj_snapshot_pauli.json");
qobj_snapshots["matrix"] =
AER::Test::Utilities::load_qobj("../../../test/data/qobj_snapshot_matrix.json");
using State = AER::QubitVector::State;
using Engine = AER::Base::Engine<QV::QubitVector>;
AER::Base::Controller<Engine, State> sim;
WHEN("we get the expected results"){
auto expected_result = R"({
"final":[[[0.7071067811865476,0.0],[0.0,0.0],[0.0,0.0],[0.7071067811865475,0.0]]],
"initial":[[[1.0,0.0],[0.0,0.0],[0.0,0.0],[0.0,0.0]]],
"middle":[[[0.7071067811865476,0.0],[0.7071067811865475,0.0],[0.0,0.0],[0.0,0.0]]]
})"_json;
THEN("the state simulator should pass"){
auto result = sim.execute(qobj_snapshots["state"]);
result = result["result"][0]["data"]["snapshots"]["state"];
REQUIRE(result == expected_result);
}
THEN("the probs simulator should pass"){
REQUIRE(false);
}
THEN("the pauli simulator should pass"){
REQUIRE(false);
}
THEN("the unitary matrix simulator should pass"){
REQUIRE(false);
}
}
}
}
} // End of namepsace AER::Test

11
test/src/utils.hpp Normal file
View File

@ -0,0 +1,11 @@
#include <string>
#include "framework/json.hpp"
namespace AER::Test::Utilities {
// QObj Utilities
json_t load_qobj(const std::string& filename){
return JSON::load(filename);
}
}

13359
test/third-party/catch.hpp vendored Normal file

File diff suppressed because it is too large Load Diff