Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ORNL Quantum Computing Institute
xacc
Commits
7612e3ad
Commit
7612e3ad
authored
Aug 09, 2017
by
Mccaskey, Alex
Browse files
Updating AcceleratorBuffer to have getExpectationValueZ method
parent
f5e68b73
Changes
3
Hide whitespace changes
Inline
Side-by-side
impls/rigetti/examples/h2_exp_vals_scaffold_rigetti.cpp
View file @
7612e3ad
...
...
@@ -108,25 +108,25 @@ int main (int argc, char** argv) {
for (float theta = -pi; theta <= pi; theta += .2) {
g1Term(qubitReg, theta);
auto e1 = qubitReg->get
Average
();
auto e1 = qubitReg->get
ExpectationValueZ
();
qubitReg->resetBuffer();
// G2 Term, same as g1 so just use it
g2Term(qubitReg, theta);
auto e2 = qubitReg->get
Average
();
auto e2 = qubitReg->get
ExpectationValueZ
();
qubitReg->resetBuffer();
// G3 Term, same as g1 so just use it
g3Term(qubitReg, theta);
auto e3 = qubitReg->get
Average
();
auto e3 = qubitReg->get
ExpectationValueZ
();
qubitReg->resetBuffer();
g4Term(qubitReg, theta);
auto e4 = qubitReg->get
Average
();
auto e4 = qubitReg->get
ExpectationValueZ
();
qubitReg->resetBuffer();
g5Term(qubitReg, theta);
auto e5 = qubitReg->get
Average
();
auto e5 = qubitReg->get
ExpectationValueZ
();
qubitReg->resetBuffer();
file << theta << "
,
" << e1 << "
,
" << e2 << "
,
" << e3 << "
,
" << e4 << "
,
" << e5 << "
\
n
";
...
...
quantum/aqc/accelerator/AQCAcceleratorBuffer.hpp
View file @
7612e3ad
...
...
@@ -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
)];
}
...
...
xacc/accelerator/AcceleratorBuffer.hpp
View file @
7612e3ad
...
...
@@ -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
;
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment