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 BlochSphere.h
QuantumState.h QuantumState.h
Qubit.h Qubit.h
QuantumGate.h
QuantumOperator.h QuantumOperator.h
QuantumCircuitReader.h QuantumCircuitReader.h
QuantumCircuitElement.h QuantumSimulator.h
QuantumWire.h circuit_elements/QuantumCircuitElement.h
QuantumTerminal.h circuit_elements/QuantumWire.h
BasicQuantumGates.h circuit_elements/QuantumGate.h
circuit_elements/QuantumTerminal.h
circuit_elements/BasicQuantumGates.h
visuals/BlochSphereNode.h visuals/BlochSphereNode.h
visuals/QuantumCircuitNode.h visuals/QuantumCircuitNode.h
visuals/QuantumCircuitElementNode.h visuals/QuantumCircuitElementNode.h
visuals/QuantumGateNode.h visuals/QuantumGateNode.h
visuals/QuantumWireNode.h visuals/QuantumWireNode.h
visuals/QuantumTerminalNode.h visuals/QuantumTerminalNode.h
visuals/QuantumStateNode.h
) )
list(APPEND SOURCES list(APPEND SOURCES
@ -25,19 +27,21 @@ list(APPEND SOURCES
BlochSphere.cpp BlochSphere.cpp
QuantumState.cpp QuantumState.cpp
Qubit.cpp Qubit.cpp
QuantumGate.cpp
QuantumOperator.cpp QuantumOperator.cpp
QuantumCircuitReader.cpp QuantumCircuitReader.cpp
QuantumCircuitElement.cpp QuantumSimulator.cpp
QuantumWire.cpp circuit_elements/QuantumGate.cpp
QuantumTerminal.cpp circuit_elements/QuantumCircuitElement.cpp
BasicQuantumGates.cpp circuit_elements/QuantumWire.cpp
circuit_elements/QuantumTerminal.cpp
circuit_elements/BasicQuantumGates.cpp
visuals/BlochSphereNode.cpp visuals/BlochSphereNode.cpp
visuals/QuantumCircuitNode.cpp visuals/QuantumCircuitNode.cpp
visuals/QuantumCircuitElementNode.cpp visuals/QuantumCircuitElementNode.cpp
visuals/QuantumGateNode.cpp visuals/QuantumGateNode.cpp
visuals/QuantumWireNode.cpp visuals/QuantumWireNode.cpp
visuals/QuantumTerminalNode.cpp visuals/QuantumTerminalNode.cpp
visuals/QuantumStateNode.cpp
visuals/BlochSphereNode.cpp visuals/BlochSphereNode.cpp
) )
@ -45,6 +49,7 @@ add_library(${PLUGIN_NAME} SHARED ${SOURCES} ${HEADERS})
target_include_directories(${PLUGIN_NAME} PUBLIC target_include_directories(${PLUGIN_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/circuit_elements
${CMAKE_CURRENT_SOURCE_DIR}/visuals ${CMAKE_CURRENT_SOURCE_DIR}/visuals
) )
target_link_libraries(${PLUGIN_NAME} PUBLIC core visual_elements ntk_math publishing) 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; return mWires;
} }
QuantumState QuantumCircuit::getInputState() const
{
QuantumState state;
for (const auto input : mInputTerminals)
{
state.addValue(input->getValue());
}
return state;
}
void QuantumCircuit::buildWireConnections() void QuantumCircuit::buildWireConnections()
{ {
for (const auto& wire : mWires) for (const auto& wire : mWires)

View file

@ -4,6 +4,7 @@
#include "QuantumGate.h" #include "QuantumGate.h"
#include "QuantumTerminal.h" #include "QuantumTerminal.h"
#include "QuantumWire.h" #include "QuantumWire.h"
#include "QuantumState.h"
#include <vector> #include <vector>
@ -28,6 +29,8 @@ public:
const std::vector<QuantumWire*>& getQuantumWires() const; const std::vector<QuantumWire*>& getQuantumWires() const;
QuantumState getInputState() const;
private: private:
bool connectivityIsValid() const; 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; 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 "Qubit.h"
#include <vector> #include <vector>
#include <string>
class QuantumState class QuantumState
{ {
public:
void addValue(const Qubit& data)
{
mState.push_back(data);
}
const std::vector<Qubit>& getData() const; const std::vector<Qubit>& getData() const;
std::string toString() const;
private: private:
std::vector<Qubit> mState; std::vector<Qubit> mState;
}; };

View file

@ -1,5 +1,7 @@
#include "Qubit.h" #include "Qubit.h"
#include <sstream>
Qubit::Qubit(const ComplexNumber& alpha, const ComplexNumber& beta) Qubit::Qubit(const ComplexNumber& alpha, const ComplexNumber& beta)
: mAlpha(alpha), : mAlpha(alpha),
mBeta(beta) mBeta(beta)
@ -26,3 +28,11 @@ bool Qubit::isIn1State() const
{ {
return mBeta.getReal() == 1.0 && mAlpha.getMagnitude() == 0.0; 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 "ComplexNumber.h"
#include <string>
class Qubit class Qubit
{ {
public: public:
@ -14,6 +16,8 @@ public:
bool isIn0State() const; bool isIn0State() const;
bool isIn1State() const; bool isIn1State() const;
std::string toString(std::size_t precision=3) const;
private: private:
ComplexNumber mAlpha; ComplexNumber mAlpha;
ComplexNumber mBeta; ComplexNumber mBeta;

View file

@ -2,6 +2,7 @@
list(APPEND UNIT_TEST_FILES list(APPEND UNIT_TEST_FILES
TestBlochSphereNode.cpp TestBlochSphereNode.cpp
TestQuantumCircuitParsing.cpp TestQuantumCircuitParsing.cpp
TestQuantumSimulator.cpp
) )
add_executable(quantum_computing_unit_tests ${CMAKE_SOURCE_DIR}/test/test_runner.cpp ${UNIT_TEST_FILES}) 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());
}