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
6703c3ea
Unverified
Commit
6703c3ea
authored
Jun 12, 2020
by
Mccaskey, Alex
Committed by
GitHub
Jun 12, 2020
Browse files
Merge pull request #202 from tnguyen-ornl/tnguyen/qpp-exp
Fixed exp-val-z calculation for the Qpp Accelerator
parents
996f6bb1
9ac4b78b
Changes
3
Hide whitespace changes
Inline
Side-by-side
quantum/plugins/qpp/accelerator/QppAccelerator.cpp
View file @
6703c3ea
...
...
@@ -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
();
}
}
...
...
quantum/plugins/qpp/accelerator/QppVisitor.cpp
View file @
6703c3ea
...
...
@@ -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_measureB
its
)
?
1.0
:
-
1.0
)
*
std
::
norm
(
m
_stateVec
[
i
]);
result
+=
(
hasEvenParity
(
i
,
in_b
its
)
?
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
...
...
quantum/plugins/qpp/accelerator/QppVisitor.hpp
View file @
6703c3ea
...
...
@@ -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
;
...
...
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