Commit 7612e3ad authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Updating AcceleratorBuffer to have getExpectationValueZ method

parent f5e68b73
......@@ -108,25 +108,25 @@ int main (int argc, char** argv) {
for (float theta = -pi; theta <= pi; theta += .2) {
g1Term(qubitReg, theta);
auto e1 = qubitReg->getAverage();
auto e1 = qubitReg->getExpectationValueZ();
qubitReg->resetBuffer();
// G2 Term, same as g1 so just use it
g2Term(qubitReg, theta);
auto e2 = qubitReg->getAverage();
auto e2 = qubitReg->getExpectationValueZ();
qubitReg->resetBuffer();
// G3 Term, same as g1 so just use it
g3Term(qubitReg, theta);
auto e3 = qubitReg->getAverage();
auto e3 = qubitReg->getExpectationValueZ();
qubitReg->resetBuffer();
g4Term(qubitReg, theta);
auto e4 = qubitReg->getAverage();
auto e4 = qubitReg->getExpectationValueZ();
qubitReg->resetBuffer();
g5Term(qubitReg, theta);
auto e5 = qubitReg->getAverage();
auto e5 = qubitReg->getExpectationValueZ();
qubitReg->resetBuffer();
file << theta << ", " << e1 << ", " << e2 << ", " << e3 << ", " << e4 << ", " << e5 << "\n";
......
......@@ -97,7 +97,7 @@ public:
*
* @param emb The minor graph embedding
*/
void setEmbedding(Embedding emb) {
void setEmbedding(Embedding& emb) {
embedding = emb;
}
......@@ -106,53 +106,108 @@ public:
*
* @return emb The minor graph embedding
*/
Embedding getEmbedding() {
const Embedding& getEmbedding() const {
return embedding;
}
void setEnergies(std::vector<double> en) {
/**
* Set the energies produced by the AQC Accelator execution.
*
* @param en The energies observed
*/
void setEnergies(std::vector<double>& en) {
energies = en;
}
std::vector<double> getEnergies() {
/**
* Return the energies.
*
* @return energes The energies
*/
const std::vector<double>& getEnergies() const {
return energies;
}
void setNumberOfOccurrences(std::vector<int> en) {
numOccurrences = en;
/**
* Set the number of occurrences of each bit state and energe.
* @param nOcc The number of occurrences
*/
void setNumberOfOccurrences(std::vector<int>& numOcc) {
numOccurrences = numOcc;
}
std::vector<int> getNumberOfOccurrences() {
/**
* Return the number of occurrences of each bit state / energy.
*
* @return numOccurrences The number of occurrences
*/
const std::vector<int>& getNumberOfOccurrences() const {
return numOccurrences;
}
void setActiveVariableIndices(std::vector<int> activeVars) {
/**
* Set the active qubit variable indices.
*
* @param activeVars The active indices.
*/
void setActiveVariableIndices(std::vector<int>& activeVars) {
activeVarIndices = activeVars;
}
std::vector<int> getActiveVariableIndices() {
/**
* Get the active variable indices.
*
* @return activeVars The active indices
*/
const std::vector<int>& getActiveVariableIndices() const {
return activeVarIndices;
}
int getNumberOfExecutions() {
/**
* Return the number of executions.
*
* @return numExec The number of executions
*
*/
const int getNumberOfExecutions() const {
return std::accumulate(numOccurrences.rbegin(), numOccurrences.rend(), 0);
}
boost::dynamic_bitset<> getLowestEnergyMeasurement() {
/**
* Return the bit string corresponding to the lowest observed energy.
*
* @return bitStr The bits corresponding to the lowest energy
*/
const boost::dynamic_bitset<>& getLowestEnergyMeasurement() {
auto minIt = std::min_element(energies.begin(), energies.end());
return measurements[std::distance(energies.begin(), minIt)];
}
double getLowestEnergy() {
/**
* Return the lowest energy observed.
*
* @return energy The lowest energy
*/
const double getLowestEnergy() const {
return *std::min_element(energies.begin(), energies.end());
}
double getMostProbableEnergy() {
/**
* Return the most probable energy.
*
* @return energy The most probable energy.
*/
const double getMostProbableEnergy() const {
auto maxIt = std::max_element(numOccurrences.begin(), numOccurrences.end());
return energies[std::distance(numOccurrences.begin(), maxIt)];
}
boost::dynamic_bitset<> getMostProbableMeasurement() {
/**
* Return the most probable bit string
*
* @return bitStr The bits
*/
const boost::dynamic_bitset<>& getMostProbableMeasurement() const {
auto maxIt = std::max_element(numOccurrences.begin(), numOccurrences.end());
return measurements[std::distance(numOccurrences.begin(), maxIt)];
}
......
......@@ -103,34 +103,76 @@ public:
AcceleratorBuffer(const std::string& str, const int N) :
bufferId(str), bits(std::vector<AcceleratorBit>(N)) {
}
/**
* The Constructor, takes as input the name of this buffer,
* and the bit indices to model.
*
* @param str The name of the buffer
* @param firstIndex The first bit index
* @param indices The remaining bit indices
*/
template<typename ... Indices>
AcceleratorBuffer(const std::string& str, int firstIndex,
Indices ... indices) :
bufferId(str), bits(
std::vector<AcceleratorBit>(1 + sizeof...(indices))) {
}
int size() {
/**
* Return the number of bits in this buffer.
*
* @return size The number of bits in this buffer
*/
const int size() const {
return bits.size();
}
std::string name() {
/**
* Return this AcceleratorBuffer's name
*
* @return name The name of this AcceleratorBuffer
*/
const std::string name() const {
return bufferId;
}
/**
* Reset the stored measured bit strings.
*/
virtual void resetBuffer() {
measurements.clear();
}
/**
* Update the Bit State for the bit at the give index
*
* @param idx The index of the bit
* @param zeroOrOne The measurement result
*/
void updateBit(const int idx, int zeroOrOne) {
bits[idx].update(zeroOrOne);
}
/**
* Add a measurement result to this Buffer
*
* @param measurement The measurement result
*/
virtual void appendMeasurement(const boost::dynamic_bitset<>& measurement) {
measurements.push_back(measurement);
}
virtual double getAverage() const {
//std::assert(measurements.size()>0);
/**
* Compute and return the expectation value with respect
* to the Pauli-Z operator. Here we provide a base implementation
* based on an ensemble of measurement results. Subclasses
* are free to implement this as they see fit, ie, for simulators
* use the wavefunction.
*
* @return expVal The expectation value
*/
virtual const double getExpectationValueZ() const {
std::stringstream ss;
double aver = 0.;
long n_measurements = measurements.size();
......@@ -159,28 +201,56 @@ public:
aver -= p;
}
}
// XACCInfo(ss.str());
return aver;
}
/**
* Return the bit state for the given bit.
*
* @param idx The index of the bit
* @return bitState the state of the bit, a 0, 1, or UNKNOWN
*/
AcceleratorBitState getAcceleratorBitState(const int idx) {
return bits[idx].getState();
}
/**
* Print information about this AcceleratorBuffer to standard out.
*
*/
virtual void print() {
}
/**
* Print information about this AcceleratorBuffer to the
* given output stream.
*
* @param stream Stream to write the buffer to.
*/
virtual void print(std::ostream& stream) {
}
/**
* The Destructor
*/
virtual ~AcceleratorBuffer() {
}
protected:
/**
* Reference to the Accelerator measurement result bit strings
*/
std::vector<boost::dynamic_bitset<>> measurements;
/**
* The name of this AcceleratorBuffer
*/
std::string bufferId;
/**
* The set of AcceleratorBits.
*/
std::vector<AcceleratorBit> bits;
};
......
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