Add simple simulator stub.

This commit is contained in:
jmsgrogan 2023-01-26 15:16:46 +00:00
parent a74dfd5f5f
commit 426ea55b3b
23 changed files with 139 additions and 11 deletions

View file

@ -5,19 +5,21 @@ list(APPEND HEADERS
BlochSphere.h
QuantumState.h
Qubit.h
QuantumGate.h
QuantumOperator.h
QuantumCircuitReader.h
QuantumCircuitElement.h
QuantumWire.h
QuantumTerminal.h
BasicQuantumGates.h
QuantumSimulator.h
circuit_elements/QuantumCircuitElement.h
circuit_elements/QuantumWire.h
circuit_elements/QuantumGate.h
circuit_elements/QuantumTerminal.h
circuit_elements/BasicQuantumGates.h
visuals/BlochSphereNode.h
visuals/QuantumCircuitNode.h
visuals/QuantumCircuitElementNode.h
visuals/QuantumGateNode.h
visuals/QuantumWireNode.h
visuals/QuantumTerminalNode.h
visuals/QuantumStateNode.h
)
list(APPEND SOURCES
@ -25,19 +27,21 @@ list(APPEND SOURCES
BlochSphere.cpp
QuantumState.cpp
Qubit.cpp
QuantumGate.cpp
QuantumOperator.cpp
QuantumOperator.cpp
QuantumCircuitReader.cpp
QuantumCircuitElement.cpp
QuantumWire.cpp
QuantumTerminal.cpp
BasicQuantumGates.cpp
QuantumSimulator.cpp
circuit_elements/QuantumGate.cpp
circuit_elements/QuantumCircuitElement.cpp
circuit_elements/QuantumWire.cpp
circuit_elements/QuantumTerminal.cpp
circuit_elements/BasicQuantumGates.cpp
visuals/BlochSphereNode.cpp
visuals/QuantumCircuitNode.cpp
visuals/QuantumCircuitElementNode.cpp
visuals/QuantumGateNode.cpp
visuals/QuantumWireNode.cpp
visuals/QuantumTerminalNode.cpp
visuals/QuantumStateNode.cpp
visuals/BlochSphereNode.cpp
)
@ -45,6 +49,7 @@ add_library(${PLUGIN_NAME} SHARED ${SOURCES} ${HEADERS})
target_include_directories(${PLUGIN_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/circuit_elements
${CMAKE_CURRENT_SOURCE_DIR}/visuals
)
target_link_libraries(${PLUGIN_NAME} PUBLIC core visual_elements ntk_math publishing)

View file

@ -48,6 +48,16 @@ const std::vector<QuantumWire*>& QuantumCircuit::getQuantumWires() const
return mWires;
}
QuantumState QuantumCircuit::getInputState() const
{
QuantumState state;
for (const auto input : mInputTerminals)
{
state.addValue(input->getValue());
}
return state;
}
void QuantumCircuit::buildWireConnections()
{
for (const auto& wire : mWires)

View file

@ -4,6 +4,7 @@
#include "QuantumGate.h"
#include "QuantumTerminal.h"
#include "QuantumWire.h"
#include "QuantumState.h"
#include <vector>
@ -28,6 +29,8 @@ public:
const std::vector<QuantumWire*>& getQuantumWires() const;
QuantumState getInputState() const;
private:
bool connectivityIsValid() const;

View file

@ -0,0 +1,25 @@
#include "QuantumSimulator.h"
#include "QuantumCircuit.h"
const QuantumState& QuantumSimulator::getInputState() const
{
return mInputState;
}
const QuantumState& QuantumSimulator::getOutputState() const
{
return mOutputState;
}
void QuantumSimulator::setCircuit(QuantumCircuit* circuit)
{
mCircuit = circuit;
}
void QuantumSimulator::run()
{
mInputState = mCircuit->getInputState();
mOutputState = mInputState;
}

View file

@ -0,0 +1,23 @@
#pragma once
#include "QuantumState.h"
class QuantumCircuit;
class QuantumSimulator
{
public:
const QuantumState& getInputState() const;
const QuantumState& getOutputState() const;
void setCircuit(QuantumCircuit* circuit);
void run();
public:
QuantumState mInputState;
QuantumState mOutputState;
QuantumCircuit* mCircuit{ nullptr };
};

View file

@ -4,3 +4,14 @@ const std::vector<Qubit>& QuantumState::getData() const
{
return mState;
}
std::string QuantumState::toString() const
{
std::string out;
std::size_t count{ 0 };
for (const auto& qubit : mState)
{
out += "|" + qubit.toString() + "\n";
}
return out;
}

View file

@ -3,11 +3,20 @@
#include "Qubit.h"
#include <vector>
#include <string>
class QuantumState
{
public:
void addValue(const Qubit& data)
{
mState.push_back(data);
}
const std::vector<Qubit>& getData() const;
std::string toString() const;
private:
std::vector<Qubit> mState;
};

View file

@ -1,5 +1,7 @@
#include "Qubit.h"
#include <sstream>
Qubit::Qubit(const ComplexNumber& alpha, const ComplexNumber& beta)
: mAlpha(alpha),
mBeta(beta)
@ -26,3 +28,11 @@ bool Qubit::isIn1State() const
{
return mBeta.getReal() == 1.0 && mAlpha.getMagnitude() == 0.0;
}
std::string Qubit::toString(std::size_t precision) const
{
std::stringstream sstr;
sstr.precision(precision);
sstr << "alpha " << mAlpha.getReal() << " " << mAlpha.getImaginary() << "i , beta " << mBeta.getReal() << " " << mBeta.getImaginary() << "i";
return sstr.str();
}

View file

@ -2,6 +2,8 @@
#include "ComplexNumber.h"
#include <string>
class Qubit
{
public:
@ -14,6 +16,8 @@ public:
bool isIn0State() const;
bool isIn1State() const;
std::string toString(std::size_t precision=3) const;
private:
ComplexNumber mAlpha;
ComplexNumber mBeta;

View file

@ -2,6 +2,7 @@
list(APPEND UNIT_TEST_FILES
TestBlochSphereNode.cpp
TestQuantumCircuitParsing.cpp
TestQuantumSimulator.cpp
)
add_executable(quantum_computing_unit_tests ${CMAKE_SOURCE_DIR}/test/test_runner.cpp ${UNIT_TEST_FILES})

View file

@ -0,0 +1,27 @@
#include "TestFramework.h"
#include "TestUtils.h"
#include "TestRenderUtils.h"
#include "QuantumCircuitReader.h"
#include "QuantumCircuitNode.h"
#include "QuantumCircuit.h"
#include "QuantumState.h"
#include "QuantumSimulator.h"
#include "File.h"
TEST_CASE(TestQuantumSimulator, "quantum_computing")
{
QuantumCircuitReader reader;
auto circuit = reader.read(TestUtils::getTestDataDir() / "quantum_circuit.dat");
QuantumSimulator simulator;
simulator.setCircuit(circuit.get());
simulator.run();
auto input_state = simulator.getInputState();
File file(TestUtils::getTestOutputDir(__FILE__) / "out.dat");
file.writeText(input_state.toString());
}