Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ORNL Quantum Computing Institute
xacc
Commits
78901a96
Commit
78901a96
authored
Jun 29, 2017
by
Mccaskey, Alex
Browse files
Fixing bug in GateFunction evaluateVariableParameters
parent
0262a1d4
Changes
5
Hide whitespace changes
Inline
Side-by-side
impls/rigetti/RigettiAccelerator.cpp
View file @
78901a96
...
...
@@ -240,6 +240,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
std
::
string
res
=
extract
<
std
::
string
>
(
main_namespace
[
"extractStr"
]);
std
::
string
measStr
=
extract
<
std
::
string
>
(
main_namespace
[
"measStr"
]);
std
::
cout
<<
"MEASUREMENT STR: "
<<
measStr
<<
"
\n
"
;
Py_Finalize
();
int
i
=
0
;
...
...
@@ -328,6 +329,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
ss
<<
postResponse
.
content
.
rdbuf
();
std
::
string
resp_str
=
ss
.
str
();
std
::
cout
<<
"Rigetti Response Str:
\n
"
<<
resp_str
<<
"
\n
"
;
int
i_qbit
=
-
1
;
boost
::
dynamic_bitset
<>
outcome
(
buffer
->
size
());
...
...
@@ -358,7 +360,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
for
(
std
::
string
tmp
;
std
::
getline
(
ss
,
tmp
,
','
);
){
oss
<<
tmp
<<
std
::
endl
;
}
XACCInfo
(
oss
.
str
());
//
XACCInfo(oss.str());
}
else
if
(
type
==
"ping"
||
type
==
"version"
)
{
...
...
impls/scaffold/ScaffoldCompiler.cpp
View file @
78901a96
...
...
@@ -136,10 +136,9 @@ std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src) {
kernelSource
=
src
;
// Replace the __qpu__ attribute with module
if
(
boost
::
contains
(
kernelSource
,
"__qpu__"
))
{
kernelSource
.
erase
(
kernelSource
.
find
(
"__qpu__"
),
7
);
kernelSource
=
std
::
string
(
"module"
)
+
kernelSource
;
std
::
cout
<<
"
\n
"
<<
kernelSource
<<
"
\n
"
;
boost
::
replace_all
(
kernelSource
,
"__qpu__"
,
"module"
);
}
// Create a temporary scaffold source file
...
...
impls/scaffold/tests/ScaffoldCompilerTester.cpp
View file @
78901a96
...
...
@@ -194,25 +194,25 @@ BOOST_AUTO_TEST_CASE(checkTeleportWithFunctions) {
BOOST_AUTO_TEST_CASE
(
checkVQEExample
)
{
const
std
::
string
src
(
""
"
module
initializeState(qbit qreg[2], float theta) {
\n
"
"
__qpu__
initializeState(qbit qreg[2], float theta) {
\n
"
" Rx(qreg[0], 1.57079);
\n
"
" Ry(qreg[1], 1.57079);
\n
"
" Rx(qreg[0],
-1.57079
);
\n
"
" CNOT(qreg[
0
], qreg[
1
]);
\n
"
" Rx(qreg[0],
7.8539752
);
\n
"
" CNOT(qreg[
1
], qreg[
0
]);
\n
"
" Rz(qreg[0], theta);
\n
"
" CNOT(qreg[
0
], qreg[
1
]);
\n
"
" Ry(qreg[1],
-1.57079
);
\n
"
" CNOT(qreg[
1
], qreg[
0
]);
\n
"
" Ry(qreg[1],
7.8539752
);
\n
"
" Rx(qreg[0], 1.57079);
\n
"
"}
\n
"
""
"
module
g1Term (qbit qreg[2], float theta) {
\n
"
"
__qpu__
g1Term (qbit qreg[2], float theta) {
\n
"
" initializeState(qreg, theta);
\n
"
" cbit creg[2];
\n
"
" creg[0] = MeasZ(qreg[0]);
\n
"
" creg[1] = MeasZ(qreg[1]);
\n
"
" creg[0] = MeasZ(qreg[0]);
\n
"
"}
\n
"
""
"
module
g4Term(qbit qreg[2], float theta) {
\n
"
"
__qpu__
g4Term(qbit qreg[2], float theta) {
\n
"
" initializeState(qreg, theta);
\n
"
" cbit creg[2];
\n
"
" Rx(qreg[0], -1.57079);
\n
"
...
...
@@ -221,28 +221,41 @@ BOOST_AUTO_TEST_CASE(checkVQEExample) {
" creg[1] = MeasZ(qreg[1]);
\n
"
"}
\n
"
""
"
module
g5Term(qbit qreg[2], float theta) {
\n
"
"
__qpu__
g5Term(qbit qreg[2], float theta) {
\n
"
" initializeState(qreg, theta);
\n
"
" cbit creg[2];
\n
"
" H(qreg[0]);
\n
"
" H(qreg[1]);
\n
"
" creg[0] = MeasZ(qreg[0]);
\n
"
" creg[1] = MeasZ(qreg[1]);
\n
"
"}
\n
"
""
);
"}
\n
"
);
ScaffoldCompiler
compiler
;
auto
ir
=
compiler
.
compile
(
src
);
// for (auto k : ir->getKernels()) {
// JsonVisitor v(k);
// std::cout << v.write() << "\n";
// }
JsonVisitor
visitor
(
ir
->
getKernels
());
std
::
cout
<<
"HI:
\n
"
<<
visitor
.
write
()
<<
"
\n
"
;
auto
k
=
ir
->
getKernel
(
"initializeState"
);
xacc
::
InstructionParameter
p
(
22.2
);
std
::
vector
<
xacc
::
InstructionParameter
>
pars
{
p
};
k
->
evaluateVariableParameters
(
pars
);
JsonVisitor
v
(
k
);
std
::
cout
<<
"AfterParams:
\n
"
<<
v
.
write
()
<<
"
\n
"
;
xacc
::
InstructionParameter
p2
(
44.2
);
std
::
vector
<
xacc
::
InstructionParameter
>
pars2
{
p2
};
k
->
evaluateVariableParameters
(
pars2
);
JsonVisitor
v2
(
k
);
std
::
cout
<<
"AfterParams2:
\n
"
<<
v2
.
write
()
<<
"
\n
"
;
}
/*
...
...
quantum/gate/ir/GateFunction.hpp
View file @
78901a96
...
...
@@ -56,6 +56,11 @@ protected:
std
::
vector
<
InstructionParameter
>
parameters
;
/**
* Map of Instruction Index to ( Instruction's Runtime Parameter Index, Dependent Variable name)
*/
std
::
map
<
int
,
std
::
pair
<
int
,
std
::
string
>>
cachedVariableInstructions
;
public:
/**
...
...
@@ -146,8 +151,7 @@ public:
virtual
InstructionParameter
getParameter
(
const
int
idx
)
{
if
(
idx
+
1
>
parameters
.
size
())
{
XACCError
(
"Invalid Parameter requested."
);
XACCError
(
"Invalid Parameter requested."
);
}
return
parameters
[
idx
];
...
...
@@ -155,8 +159,7 @@ public:
virtual
void
setParameter
(
const
int
idx
,
InstructionParameter
&
p
)
{
if
(
idx
+
1
>
parameters
.
size
())
{
XACCError
(
"Invalid Parameter requested."
);
XACCError
(
"Invalid Parameter requested."
);
}
parameters
[
idx
]
=
p
;
...
...
@@ -170,45 +173,60 @@ public:
return
parameters
.
size
();
}
std
::
map
<
int
,
int
>
cachedVariableInstructions
;
virtual
void
evaluateVariableParameters
(
std
::
vector
<
InstructionParameter
>
runtimeParameters
)
{
virtual
void
evaluateVariableParameters
(
std
::
vector
<
InstructionParameter
>
runtimeParameters
)
{
std
::
map
<
std
::
string
,
InstructionParameter
>
varToValMap
;
int
i
=
0
;
for
(
auto
funcParam
:
parameters
)
{
varToValMap
.
insert
(
std
::
make_pair
(
boost
::
get
<
std
::
string
>
(
funcParam
),
runtimeParameters
[
i
]));
i
++
;
}
for
(
const
auto
&
gateIdVarName
:
cachedVariableInstructions
)
{
auto
inst
=
getInstruction
(
gateIdVarName
.
first
);
auto
varInstDependsOn
=
gateIdVarName
.
second
.
second
;
auto
instParamIdx
=
gateIdVarName
.
second
.
first
;
int
indexOfRuntimeParam
,
counter
=
0
;
for
(
auto
p
:
parameters
)
{
if
(
boost
::
get
<
std
::
string
>
(
p
)
==
varInstDependsOn
)
{
indexOfRuntimeParam
=
counter
;
break
;
}
counter
++
;
}
inst
->
setParameter
(
instParamIdx
,
runtimeParameters
[
indexOfRuntimeParam
]);
}
i
=
0
;
for
(
auto
inst
:
instructions
)
{
if
(
inst
->
isComposite
())
{
std
::
dynamic_pointer_cast
<
Function
>
(
inst
)
->
evaluateVariableParameters
(
runtimeParameters
);
}
else
{
if
(
inst
->
isParameterized
()
&&
inst
->
getName
()
!=
"Measure"
)
{
for
(
int
i
=
0
;
i
<
inst
->
nParameters
();
i
++
)
{
auto
param
=
inst
->
getParameter
(
i
);
// See if this is a string parameter
if
(
param
.
which
()
==
3
)
{
auto
variable
=
boost
::
get
<
std
::
string
>
(
param
);
// Get index
auto
it
=
std
::
find
(
parameters
.
begin
(),
parameters
.
end
(),
param
);
if
(
it
==
parameters
.
end
())
{
XACCError
(
"Variable "
+
variable
+
" not found in Function parameters."
);
}
else
{
auto
index
=
std
::
distance
(
parameters
.
begin
(),
it
);
inst
->
setParameter
(
index
,
runtimeParameters
[
index
]);
cachedVariableInstructions
.
insert
(
std
::
make_pair
(
i
,
index
));
}
}
else
{
// See if we have a cached instruction
if
(
cachedVariableInstructions
.
find
(
i
)
!=
cachedVariableInstructions
.
end
())
{
auto
idx
=
cachedVariableInstructions
[
i
];
inst
->
setParameter
(
idx
,
runtimeParameters
[
idx
]);
}
}
}
else
if
(
inst
->
isParameterized
()
&&
inst
->
getName
()
!=
"Measure"
)
{
for
(
int
j
=
0
;
j
<
inst
->
nParameters
();
++
j
)
{
auto
instParam
=
inst
->
getParameter
(
j
);
if
(
instParam
.
which
()
==
3
)
{
// This is a variable
auto
variable
=
boost
::
get
<
std
::
string
>
(
instParam
);
auto
runtimeParameter
=
varToValMap
[
variable
];
inst
->
setParameter
(
j
,
runtimeParameter
);
cachedVariableInstructions
.
insert
(
std
::
make_pair
(
i
,
std
::
make_pair
(
j
,
variable
)));
}
}
}
i
++
;
}
}
...
...
quantum/gate/utils/JsonVisitor.hpp
View file @
78901a96
...
...
@@ -64,7 +64,7 @@ public:
JsonVisitor
(
std
::
shared_ptr
<
xacc
::
Function
>
f
)
:
buffer
(
std
::
make_shared
<
StringBuffer
>
()),
writer
(
std
::
make_shared
<
PrettyWriter
<
StringBuffer
>>
(
*
buffer
.
get
())),
function
(
f
)
{
*
buffer
.
get
())),
function
s
{
f
}
{
}
JsonVisitor
(
std
::
vector
<
std
::
shared_ptr
<
xacc
::
Function
>>
fs
)
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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