Fix linux build.

This commit is contained in:
James Grogan 2023-01-28 16:58:26 +00:00
parent a6d92e142f
commit 9e1d951520
50 changed files with 1586 additions and 1192 deletions

View file

@ -4,6 +4,8 @@
#include "WireNode.h"
#include "LogicGateNode.h"
#include "LineNode.h"
#include "FileLogger.h"
ElectronicCircuitNode::ElectronicCircuitNode(const Transform& transform)

View file

@ -12,194 +12,194 @@
std::unique_ptr<QuantumCircuit> QuantumCircuitReader::read(const Path& path)
{
File file(path);
return read(file.readText());
File file(path);
return read(file.readText());
}
std::unique_ptr<QuantumCircuit> QuantumCircuitReader::read(const std::string& content)
{
auto circuit = std::make_unique<QuantumCircuit>();
mWorkingCircuit = circuit.get();
auto circuit = std::make_unique<QuantumCircuit>();
mWorkingCircuit = circuit.get();
std::size_t cursor = 0;
for (const auto& line : StringUtils::toLines(content))
{
onLine(line, cursor);
cursor++;
}
circuit->buildWireConnections();
return circuit;
std::size_t cursor = 0;
for (const auto& line : StringUtils::toLines(content))
{
onLine(line, cursor);
cursor++;
}
circuit->buildWireConnections();
return circuit;
}
void QuantumCircuitReader::onLine(const std::string& line, std::size_t jdx)
{
mWorkingString.clear();
std::size_t cursor = 0;
mWorkingString.clear();
std::size_t cursor = 0;
while (cursor < line.size())
{
const auto c = line[cursor];
MLOG_INFO("Working char: " << std::string(1, c));
while (cursor < line.size())
{
const auto c = line[cursor];
MLOG_INFO("Working char: " << std::string(1, c));
if (c == '|')
{
if (cursor + 1 < line.size())
{
if (line[cursor + 1] == '-')
{
onVertialQuantumWire({ cursor, jdx });
}
else if (auto ket = checkForKet(line.substr(cursor + 1, line.size() - cursor)); !ket.empty())
{
onKet({ cursor, jdx }, ket);
cursor += ket.size() + 1;
}
}
else
{
mWorkingString += c;
}
}
else if (c == '-')
{
if (cursor + 1 < line.size())
{
const auto end_id = getWireEnd(line.substr(cursor, line.size() - cursor));
MLOG_INFO("Wire: " << cursor << " with length " << end_id);
cursor += end_id - 1;
}
else
{
mWorkingString += c;
}
}
else
{
if (cursor + 1 < line.size())
{
auto gate = checkForGate(line.substr(cursor, line.size() - cursor));
onGate({ cursor, jdx }, gate);
cursor += gate.size() - 1;
}
else
{
mWorkingString += c;
}
}
cursor++;
}
onLineEnd();
if (c == '|')
{
if (cursor + 1 < line.size())
{
if (line[cursor + 1] == '-')
{
onVertialQuantumWire({ cursor, jdx });
}
else if (auto ket = checkForKet(line.substr(cursor + 1, line.size() - cursor)); !ket.empty())
{
onKet({ cursor, jdx }, ket);
cursor += ket.size() + 1;
}
}
else
{
mWorkingString += c;
}
}
else if (c == '-')
{
if (cursor + 1 < line.size())
{
const auto end_id = getWireEnd(line.substr(cursor, line.size() - cursor));
MLOG_INFO("Wire: " << cursor << " with length " << end_id);
cursor += end_id - 1;
}
else
{
mWorkingString += c;
}
}
else
{
if (cursor + 1 < line.size())
{
auto gate = checkForGate(line.substr(cursor, line.size() - cursor));
onGate({ cursor, jdx }, gate);
cursor += gate.size() - 1;
}
else
{
mWorkingString += c;
}
}
cursor++;
}
onLineEnd();
}
void QuantumCircuitReader::onLineEnd()
{
auto output_terminal = std::make_unique<QuantumTerminal>(QuantumTerminal::TerminalType::OUTPUT);
auto output_terminal = std::make_unique<QuantumTerminal>(QuantumTerminal::TerminalType::OUTPUT);
auto wire = std::make_unique<QuantumWire>(mWorkingElement, output_terminal.get());
mWorkingCircuit->addQuantumWire(std::move(wire));
auto wire = std::make_unique<QuantumWire>(mWorkingElement, output_terminal.get());
mWorkingCircuit->addQuantumWire(std::move(wire));
mWorkingCircuit->addOutputTerminal(std::move(output_terminal));
mWorkingCircuit->addOutputTerminal(std::move(output_terminal));
mWorkingElement = nullptr;
mWorkingElement = nullptr;
}
void QuantumCircuitReader::onGate(Location loc, const std::string value)
void QuantumCircuitReader::onGate(Location, const std::string value)
{
MLOG_INFO("Got gate: " << value);
MLOG_INFO("Got gate: " << value);
QuantumGatePtr gate;
QuantumGatePtr gate;
if (value == "X")
{
gate = std::make_unique<XQuantumGate>();
}
else if (value == "Z")
{
gate = std::make_unique<ZQuantumGate>();
}
if (value == "X")
{
gate = std::make_unique<XQuantumGate>();
}
else if (value == "Z")
{
gate = std::make_unique<ZQuantumGate>();
}
if (gate)
{
auto wire = std::make_unique<QuantumWire>(mWorkingElement, gate.get());
mWorkingCircuit->addQuantumWire(std::move(wire));
if (gate)
{
auto wire = std::make_unique<QuantumWire>(mWorkingElement, gate.get());
mWorkingCircuit->addQuantumWire(std::move(wire));
mWorkingElement = gate.get();
mWorkingCircuit->addLogicGate(std::move(gate));
}
mWorkingElement = gate.get();
mWorkingCircuit->addLogicGate(std::move(gate));
}
}
std::string QuantumCircuitReader::checkForGate(const std::string& segment)
{
std::string working_string;
for (const auto c : segment)
{
if (c == '-')
{
break;
}
working_string += c;
}
return working_string;
std::string working_string;
for (const auto c : segment)
{
if (c == '-')
{
break;
}
working_string += c;
}
return working_string;
}
void QuantumCircuitReader::onKet(Location loc, const std::string value)
void QuantumCircuitReader::onKet(Location, const std::string value)
{
MLOG_INFO("Got input state: " << value);
Qubit qubit;
if (value == "1")
{
qubit = Qubit({ 0.0, 0.0 }, { 1.0, 0.0 });
}
MLOG_INFO("Got input state: " << value);
Qubit qubit;
if (value == "1")
{
qubit = Qubit({ 0.0, 0.0 }, { 1.0, 0.0 });
}
auto input_terminal = std::make_unique<QuantumTerminal>(QuantumTerminal::TerminalType::INPUT);
auto input_terminal = std::make_unique<QuantumTerminal>(QuantumTerminal::TerminalType::INPUT);
mWorkingElement = input_terminal.get();
mWorkingCircuit->addInputTerminal(std::move(input_terminal));
mWorkingElement = input_terminal.get();
mWorkingCircuit->addInputTerminal(std::move(input_terminal));
}
std::size_t QuantumCircuitReader::getWireEnd(const std::string& segment)
{
std::size_t idx = 0;
for (const auto c : segment)
{
if (c != '-')
{
break;
}
idx++;
}
return idx;
std::size_t idx = 0;
for (const auto c : segment)
{
if (c != '-')
{
break;
}
idx++;
}
return idx;
}
std::string QuantumCircuitReader::checkForKet(const std::string& segment)
{
std::string working_string;
bool found{ false };
std::string working_string;
bool found{ false };
for (const auto c : segment)
{
if (c == '>')
{
found = true;
break;
}
else
{
working_string += c;
}
}
for (const auto c : segment)
{
if (c == '>')
{
found = true;
break;
}
else
{
working_string += c;
}
}
if (found)
{
return working_string;
}
else
{
return {};
}
if (found)
{
return working_string;
}
else
{
return {};
}
}
void QuantumCircuitReader::onVertialQuantumWire(Location loc)
void QuantumCircuitReader::onVertialQuantumWire(Location)
{
}
}

View file

@ -2,16 +2,15 @@
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;
}
std::string out;
for (const auto& qubit : mState)
{
out += "|" + qubit.toString() + "\n";
}
return out;
}

View file

@ -8,15 +8,15 @@
class QuantumState
{
public:
void addValue(const Qubit& data)
{
mState.push_back(data);
}
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;
std::string toString() const;
private:
std::vector<Qubit> mState;
};
std::vector<Qubit> mState;
};

View file

@ -9,6 +9,9 @@
#include "QuantumWireNode.h"
#include "QuantumGateNode.h"
#include "LatexMathExpression.h"
#include "EquationNode.h"
QuantumCircuitNode::QuantumCircuitNode(const Transform& t)
: AbstractVisualNode(t)
{
@ -17,8 +20,8 @@ QuantumCircuitNode::QuantumCircuitNode(const Transform& t)
void QuantumCircuitNode::setContent(QuantumCircuit* circuit)
{
mContent = circuit;
mContentDirty = true;
mContent = circuit;
mContentDirty = true;
}
void QuantumCircuitNode::update(SceneInfo* sceneInfo)
@ -127,4 +130,4 @@ void QuantumCircuitNode::createOrUpdateGeometry(SceneInfo*)
mWireNodes.push_back(std::move(wire_node));
}
}
}

View file

@ -7,7 +7,7 @@
#include "LatexMathExpression.h"
QuantumGateNode::QuantumGateNode(const Transform& t)
: QuantumCircuitElementNode(t)
: QuantumCircuitElementNode(t)
{
}
@ -19,93 +19,93 @@ QuantumGateNode::~QuantumGateNode()
void QuantumGateNode::setContent(QuantumGate* gate)
{
mContent = gate;
mContentDirty = true;
mContent = gate;
mContentDirty = true;
}
void QuantumGateNode::update(SceneInfo* sceneInfo)
{
if (mContentDirty)
{
createOrUpdateGeometry(sceneInfo);
mContentDirty = false;
}
if (mContentDirty)
{
createOrUpdateGeometry(sceneInfo);
mContentDirty = false;
}
}
void QuantumGateNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
void QuantumGateNode::createOrUpdateGeometry(SceneInfo*)
{
if (!mBody)
{
mBody = std::make_unique<RectangleNode>(Point(0, 0), mBodyWidth, mBodyHeight);
addChild(mBody.get());
}
if (!mBody)
{
mBody = std::make_unique<RectangleNode>(Point(0, 0), mBodyWidth, mBodyHeight);
addChild(mBody.get());
}
if (!mLabel)
{
mLabel = std::make_unique<EquationNode>(Point(mBodyWidth /3.0, mBodyHeight / 3.0));
if (!mLabel)
{
mLabel = std::make_unique<EquationNode>(Point(mBodyWidth /3.0, mBodyHeight / 3.0));
std::string label_content;
if (mContent->getGateType() == QuantumGate::GateType::X)
{
label_content = "X";
}
else if (mContent->getGateType() == QuantumGate::GateType::Y)
{
label_content = "Y";
}
else if (mContent->getGateType() == QuantumGate::GateType::Z)
{
label_content = "Z";
}
else if (mContent->getGateType() == QuantumGate::GateType::H)
{
label_content = "H";
}
else
{
label_content = "U";
}
std::string label_content;
if (mContent->getGateType() == QuantumGate::GateType::X)
{
label_content = "X";
}
else if (mContent->getGateType() == QuantumGate::GateType::Y)
{
label_content = "Y";
}
else if (mContent->getGateType() == QuantumGate::GateType::Z)
{
label_content = "Z";
}
else if (mContent->getGateType() == QuantumGate::GateType::H)
{
label_content = "H";
}
else
{
label_content = "U";
}
mLabelExpression = std::make_unique<LatexMathExpression>(label_content);
mLabel->setContent(mLabelExpression.get());
addChild(mLabel.get());
}
mLabelExpression = std::make_unique<LatexMathExpression>(label_content);
mLabel->setContent(mLabelExpression.get());
addChild(mLabel.get());
}
}
Point QuantumGateNode::getConnectionLocation(AbstractQuantumWire* wire) const
{
bool is_input{ false };
std::size_t connection_id{ 0 };
bool is_input{ false };
//std::size_t connection_id{ 0 };
for (std::size_t idx = 0; idx < mContent->getNumInputs(); idx++)
{
if (mContent->getInput(idx) == wire)
{
is_input = true;
connection_id = idx;
break;
}
}
for (std::size_t idx = 0; idx < mContent->getNumInputs(); idx++)
{
if (mContent->getInput(idx) == wire)
{
is_input = true;
//connection_id = idx;
break;
}
}
for (std::size_t idx = 0; idx < mContent->getNumOutputs(); idx++)
{
if (mContent->getOutput(idx) == wire)
{
connection_id = idx;
break;
}
}
for (std::size_t idx = 0; idx < mContent->getNumOutputs(); idx++)
{
if (mContent->getOutput(idx) == wire)
{
//connection_id = idx;
break;
}
}
Point loc;
if (is_input)
{
loc = Point(0.0, mBodyHeight/2.0);
}
else
{
loc = Point(mBodyWidth, mBodyHeight / 2.0);
}
Point loc;
if (is_input)
{
loc = Point(0.0, mBodyHeight/2.0);
}
else
{
loc = Point(mBodyWidth, mBodyHeight / 2.0);
}
loc.move(mTransform.getLocation().getX(), mTransform.getLocation().getY());
return loc;
}
loc.move(mTransform.getLocation().getX(), mTransform.getLocation().getY());
return loc;
}

View file

@ -5,56 +5,56 @@
#include "LatexMathExpression.h"
QuantumTerminalNode::QuantumTerminalNode(const Transform& transform)
: QuantumCircuitElementNode(transform)
: QuantumCircuitElementNode(transform)
{
}
void QuantumTerminalNode::setContent(QuantumTerminal* terminal)
{
mContent = terminal;
mContentDirty = true;
mContent = terminal;
mContentDirty = true;
}
void QuantumTerminalNode::update(SceneInfo* sceneInfo)
{
if (mContentDirty)
{
createOrUpdateGeometry(sceneInfo);
mContentDirty = false;
}
if (mContentDirty)
{
createOrUpdateGeometry(sceneInfo);
mContentDirty = false;
}
}
void QuantumTerminalNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
void QuantumTerminalNode::createOrUpdateGeometry(SceneInfo*)
{
if (!mLabel && mContent->getTerminalType() != QuantumTerminal::TerminalType::OUTPUT)
{
const auto value = mContent->getValue();
std::string label;
if (value.isIn0State())
{
label = "\\ket{0}";
}
else if(value.isIn1State())
{
label = "\\ket{1}";
}
else
{
label = "\\Psi";
}
if (!mLabel && mContent->getTerminalType() != QuantumTerminal::TerminalType::OUTPUT)
{
const auto value = mContent->getValue();
std::string label;
if (value.isIn0State())
{
label = "\\ket{0}";
}
else if(value.isIn1State())
{
label = "\\ket{1}";
}
else
{
label = "\\Psi";
}
mLabelExpression = std::make_unique<LatexMathExpression>(label);
mLabel = std::make_unique<EquationNode>();
mLabel->setContent(mLabelExpression.get());
mLabelExpression = std::make_unique<LatexMathExpression>(label);
mLabel = std::make_unique<EquationNode>();
mLabel->setContent(mLabelExpression.get());
addChild(mLabel.get());
}
addChild(mLabel.get());
}
}
Point QuantumTerminalNode::getConnectionLocation(AbstractQuantumWire*) const
{
auto left = mTransform.getLocation();
left.move(mWidth, mHeight/2.0);
return left;
auto left = mTransform.getLocation();
left.move(mWidth, mHeight/2.0);
return left;
}

View file

@ -4,6 +4,7 @@
#include "LineNode.h"
QuantumWireNode::QuantumWireNode(const Transform& t)
: AbstractVisualNode(t)
{
}
@ -46,7 +47,7 @@ void QuantumWireNode::update(SceneInfo* sceneInfo)
}
}
void QuantumWireNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
void QuantumWireNode::createOrUpdateGeometry(SceneInfo*)
{
if (!mLine)
{
@ -70,4 +71,4 @@ void QuantumWireNode::createOrUpdateGeometry(SceneInfo* sceneInfo)
addChild(mLine.get());
}
}
}

View file

@ -6,6 +6,12 @@
#include "QuantumCircuitNode.h"
#include "QuantumCircuit.h"
#include "QuantumWireNode.h"
#include "QuantumGateNode.h"
#include "QuantumTerminalNode.h"
#include "EquationNode.h"
#include "LatexMathExpression.h"
TEST_CASE(TestQuantumCircuitParsing, "quantum_computing")
{
@ -21,4 +27,4 @@ TEST_CASE(TestQuantumCircuitParsing, "quantum_computing")
renderer.getScene()->addNode(node.get());
renderer.writeSvg(TestUtils::getTestOutputDir(__FILE__) / "circuit.svg");
}
}