Unverified Commit 6703c3ea authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #202 from tnguyen-ornl/tnguyen/qpp-exp

Fixed exp-val-z calculation for the Qpp Accelerator
parents 996f6bb1 9ac4b78b
......@@ -181,6 +181,8 @@ namespace quantum {
void QppAccelerator::initialize(const HeterogeneousMap& params)
{
m_visitor = std::make_shared<QppVisitor>();
// Default: no shots (unless otherwise specified)
m_shots = -1;
if (params.keyExists<int>("shots"))
{
m_shots = params.get<int>("shots");
......@@ -210,57 +212,63 @@ namespace quantum {
m_visitor->finalize();
};
if (m_shots < 0)
{
runCircuit(false);
}
else
// Not possible to simulate shot count by direct sampling,
// e.g. must collapse the state vector.
if(!shotCountFromFinalStateVec(compositeInstruction))
{
// Not possible to simulate shot count by direct sampling,
// e.g. must collapse the state vector.
if(!shotCountFromFinalStateVec(compositeInstruction))
{
if (m_shots < 0)
{
runCircuit(false);
}
else
{
for (int i = 0; i < m_shots; ++i)
{
runCircuit(true);
}
}
else
{
// Index of measure bits
std::vector<size_t> measureBitIdxs;
m_visitor->initialize(buffer);
// Walk the IR tree, and visit each node
InstructionIterator it(compositeInstruction);
while (it.hasNext())
}
}
else
{
// Index of measure bits
std::vector<size_t> measureBitIdxs;
m_visitor->initialize(buffer);
// Walk the IR tree, and visit each node
InstructionIterator it(compositeInstruction);
while (it.hasNext())
{
auto nextInst = it.next();
if (nextInst->isEnabled())
{
auto nextInst = it.next();
if (nextInst->isEnabled())
if (!isMeasureGate(nextInst))
{
if (!isMeasureGate(nextInst))
{
nextInst->accept(m_visitor);
}
else
{
// Just collect the indices of measured qubit
measureBitIdxs.emplace_back(nextInst->bits()[0]);
}
nextInst->accept(m_visitor);
}
else
{
// Just collect the indices of measured qubit
measureBitIdxs.emplace_back(nextInst->bits()[0]);
}
}
// Run bit-string simulation
if (!measureBitIdxs.empty())
}
// Run bit-string simulation
if (!measureBitIdxs.empty())
{
const auto& stateVec = m_visitor->getStateVec();
if (m_shots < 0)
{
const double expectedValueZ = QppVisitor::calcExpectationValueZ(stateVec, measureBitIdxs);
buffer->addExtraInfo("exp-val-z", expectedValueZ);
}
else
{
const auto& stateVec = m_visitor->getStateVec();
// Try multi-threaded execution if there are many shots.
const bool multiThreadEnabled = (m_shots > 1024);
generateMeasureBitString(buffer, measureBitIdxs, stateVec, m_shots, multiThreadEnabled);
}
m_visitor->finalize();
}
}
m_visitor->finalize();
}
}
......
......@@ -72,7 +72,7 @@ namespace quantum {
return m_buffer->size() - in_idx - 1;
}
double QppVisitor::calcExpectationValueZ() const
double QppVisitor::calcExpectationValueZ(const KetVectorType& in_stateVec, const std::vector<qpp::idx>& in_bits)
{
const auto hasEvenParity = [](size_t x, const std::vector<size_t>& in_qubitIndices) -> bool {
size_t count = 0;
......@@ -88,9 +88,9 @@ namespace quantum {
double result = 0.0;
for(uint64_t i = 0; i < m_stateVec.size(); ++i)
for(uint64_t i = 0; i < in_stateVec.size(); ++i)
{
result += (hasEvenParity(i, m_measureBits) ? 1.0 : -1.0) * std::norm(m_stateVec[i]);
result += (hasEvenParity(i, in_bits) ? 1.0 : -1.0) * std::norm(in_stateVec[i]);
}
return result;
......@@ -270,7 +270,7 @@ namespace quantum {
if (!m_shotsMode)
{
// Not running a shot simulation, calculate the expectation value.
const double expectedValueZ = calcExpectationValueZ();
const double expectedValueZ = calcExpectationValueZ(m_stateVec, m_measureBits);
m_buffer->addExtraInfo("exp-val-z", expectedValueZ);
}
else
......
......@@ -55,9 +55,9 @@ public:
virtual std::shared_ptr<QppVisitor> clone() { return std::make_shared<QppVisitor>(); }
const KetVectorType& getStateVec() const { return m_stateVec; }
static double calcExpectationValueZ(const KetVectorType& in_stateVec, const std::vector<qpp::idx>& in_bits);
private:
qpp::idx xaccIdxToQppIdx(size_t in_idx) const;
double calcExpectationValueZ() const;
private:
std::shared_ptr<AcceleratorBuffer> m_buffer;
std::vector<qpp::idx> m_dims;
......
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