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
027ad2c2
Commit
027ad2c2
authored
Apr 18, 2017
by
Mccaskey, Alex
Browse files
adding gate instruction registration
parent
bb877d5c
Changes
9
Hide whitespace changes
Inline
Side-by-side
quantum/gate/gateqir/GateInstruction.hpp
View file @
027ad2c2
...
...
@@ -32,6 +32,7 @@
#define QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_
#include "QInstruction.hpp"
#include "Registry.hpp"
namespace
xacc
{
namespace
quantum
{
...
...
@@ -74,6 +75,10 @@ public:
std
::
vector
<
int
>
{
})
{
}
GateInstruction
(
int
id
,
int
layer
,
std
::
vector
<
int
>
qubts
)
:
gateId
(
id
),
circuitLayer
(
layer
),
gateName
(
"UNKNOWN"
),
qbits
(
qubts
)
{
}
/**
* The constructor, takes the id, name, layer, and qubits
* this instruction acts on.
...
...
@@ -144,9 +149,26 @@ public:
virtual
~
GateInstruction
()
{
}
};
}
}
/**
*/
using
GateInstructionRegistry
=
Registry
<
GateInstruction
,
int
,
int
,
std
::
vector
<
int
>>
;
/**
*/
template
<
typename
T
>
class
RegisterGateInstruction
{
public:
RegisterGateInstruction
(
const
std
::
string
&
name
)
{
GateInstructionRegistry
::
instance
()
->
add
(
name
,
(
std
::
function
<
std
::
shared_ptr
<
xacc
::
quantum
::
GateInstruction
>
(
int
,
int
,
std
::
vector
<
int
>
)
>
)
([](
int
id
,
int
layer
,
std
::
vector
<
int
>
qubits
)
{
return
std
::
make_shared
<
T
>
(
id
,
layer
,
qubits
);
}));
}
};
}
}
#endif
/* QUANTUM_GATE_GATEQIR_GATEINSTRUCTION_HPP_ */
quantum/gate/gateqir/ParameterizedGateInstruction.hpp
View file @
027ad2c2
...
...
@@ -104,6 +104,27 @@ public:
}
};
}}
/**
*/
template
<
typename
...
Params
>
using
ParameterizedGateInstructionRegistry
=
Registry
<
ParameterizedGateInstruction
<
Params
...
>
,
int
,
int
,
std
::
vector
<
int
>
,
Params
...
>
;
/**
*/
template
<
typename
T
,
typename
...
Params
>
class
RegisterParameterizedGateInstruction
{
public:
RegisterParameterizedGateInstruction
(
const
std
::
string
&
name
)
{
ParameterizedGateInstructionRegistry
<
Params
...
>::
instance
()
->
add
(
name
,
(
std
::
function
<
std
::
shared_ptr
<
xacc
::
quantum
::
ParameterizedGateInstruction
<
Params
...
>>
(
int
,
int
,
std
::
vector
<
int
>
,
Params
...)
>
)
([](
int
id
,
int
layer
,
std
::
vector
<
int
>
qubits
,
Params
...
args
)
{
return
std
::
make_shared
<
T
>
(
id
,
layer
,
qubits
,
args
...);
}));
}
};
}
}
#endif
quantum/gate/gateqir/instructions/CNOT.hpp
View file @
027ad2c2
...
...
@@ -31,7 +31,7 @@
#ifndef QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_
#define QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_
#include "
../../gateqir/
GateInstruction.hpp"
#include "GateInstruction.hpp"
class
QInstructionVisitor
;
namespace
xacc
{
...
...
@@ -42,6 +42,10 @@ namespace quantum {
*/
class
CNOT
:
public
virtual
GateInstruction
{
public:
CNOT
(
int
id
,
int
layer
,
std
::
vector
<
int
>
qbits
)
:
GateInstruction
(
id
,
layer
,
"CNOT"
,
qbits
)
{
}
CNOT
(
int
id
,
int
layer
,
int
srcqbit
,
int
tgtqbit
)
:
GateInstruction
(
id
,
layer
,
"CNOT"
,
std
::
vector
<
int
>
{
srcqbit
,
tgtqbit
})
{
...
...
@@ -52,6 +56,9 @@ public:
}
};
RegisterGateInstruction
<
CNOT
>
CNOTTEMP
(
"CNOT"
);
}
}
#endif
/* QUANTUM_GATE_GATEQIR_INSTRUCTIONS_CNOT_HPP_ */
quantum/gate/gateqir/instructions/Hadamard.hpp
View file @
027ad2c2
...
...
@@ -42,6 +42,10 @@ namespace quantum {
*/
class
Hadamard
:
public
virtual
GateInstruction
{
public:
Hadamard
(
int
id
,
int
layer
,
std
::
vector
<
int
>
qbit
)
:
GateInstruction
(
id
,
layer
,
"H"
,
qbit
)
{
}
Hadamard
(
int
id
,
int
layer
,
int
qbit
)
:
GateInstruction
(
id
,
layer
,
"H"
,
std
::
vector
<
int
>
{
qbit
})
{
}
...
...
@@ -51,6 +55,8 @@ public:
}
};
RegisterGateInstruction
<
Hadamard
>
HTEMP
(
"H"
);
}
}
...
...
quantum/gate/gateqir/instructions/Rz.hpp
View file @
027ad2c2
...
...
@@ -38,6 +38,10 @@ namespace xacc {
namespace
quantum
{
class
Rz
:
public
virtual
ParameterizedGateInstruction
<
double
>
{
public:
Rz
(
int
id
,
int
layer
,
std
::
vector
<
int
>
qbits
,
double
theta
)
:
ParameterizedGateInstruction
<
double
>
(
theta
),
GateInstruction
(
id
,
layer
,
"Rz"
,
qbits
)
{
}
Rz
(
int
id
,
int
layer
,
int
qbit
,
double
theta
)
:
ParameterizedGateInstruction
<
double
>
(
theta
),
GateInstruction
(
id
,
layer
,
"Rz"
,
std
::
vector
<
int
>
{
qbit
})
{
...
...
@@ -48,6 +52,8 @@ public:
}
};
RegisterParameterizedGateInstruction
<
Rz
,
double
>
RZTEMP
(
"Rz"
);
}
}
...
...
quantum/gate/gateqir/tests/CNOTTester.cpp
View file @
027ad2c2
...
...
@@ -59,3 +59,9 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
}
BOOST_AUTO_TEST_CASE
(
checkAutoRegistration
)
{
auto
cnot
=
GateInstructionRegistry
::
instance
()
->
create
(
"CNOT"
,
1
,
1
,
std
::
vector
<
int
>
{
0
});
BOOST_VERIFY
(
cnot
->
getId
()
==
1
);
}
quantum/gate/gateqir/tests/HadamardTester.cpp
View file @
027ad2c2
...
...
@@ -57,3 +57,10 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
}
BOOST_AUTO_TEST_CASE
(
checkAutoRegistration
)
{
auto
hadamard
=
GateInstructionRegistry
::
instance
()
->
create
(
"H"
,
1
,
1
,
std
::
vector
<
int
>
{
0
});
BOOST_VERIFY
(
hadamard
->
getId
()
==
1
);
}
quantum/gate/gateqir/tests/RzTester.cpp
View file @
027ad2c2
...
...
@@ -59,3 +59,11 @@ BOOST_AUTO_TEST_CASE(checkCreation) {
}
BOOST_AUTO_TEST_CASE
(
checkAutoRegistration
)
{
auto
rz
=
ParameterizedGateInstructionRegistry
<
double
>::
instance
()
->
create
(
"Rz"
,
1
,
1
,
std
::
vector
<
int
>
{
0
},
3.14
);
//
BOOST_VERIFY
(
rz
->
getName
()
==
"Rz"
);
BOOST_VERIFY
(
rz
->
getParameter
(
0
)
==
3.14
);
}
xacc/utils/Registry.hpp
View file @
027ad2c2
...
...
@@ -49,14 +49,14 @@ namespace xacc {
* the template parameter T.
*/
template
<
typename
T
,
typename
...
TArgs
>
class
Registry
:
public
Singleton
<
Registry
<
T
>>
{
class
Registry
:
public
Singleton
<
Registry
<
T
,
TArgs
...
>>
{
protected:
/**
* Reference to the database of creation functions
* for classes of superclass type T.
*/
std
::
map
<
std
::
string
,
std
::
function
<
std
::
shared_ptr
<
T
>
()
>>
registry
;
std
::
map
<
std
::
string
,
std
::
function
<
std
::
shared_ptr
<
T
>
(
TArgs
...
)
>>
registry
;
public:
...
...
@@ -68,7 +68,7 @@ public:
* @param f
* @return
*/
bool
add
(
const
std
::
string
&
id
,
std
::
function
<
std
::
shared_ptr
<
T
>
()
>
f
)
{
bool
add
(
const
std
::
string
&
id
,
std
::
function
<
std
::
shared_ptr
<
T
>
(
TArgs
...
)
>
f
)
{
bool
s
=
registry
.
insert
(
std
::
make_pair
(
id
,
f
)).
second
;
if
(
!
s
)
{
XACCError
(
"Could not add "
+
id
+
" to the Registry."
);
...
...
@@ -83,10 +83,10 @@ public:
* @param id
* @return
*/
std
::
shared_ptr
<
T
>
create
(
const
std
::
string
&
id
)
{
std
::
shared_ptr
<
T
>
create
(
const
std
::
string
&
id
,
TArgs
...
args
)
{
auto
search
=
registry
.
find
(
id
);
if
(
search
!=
registry
.
end
())
{
return
registry
[
id
]();
return
registry
[
id
](
args
...
);
}
else
{
XACCError
(
"Invalid Registry map id string - "
+
id
);
}
...
...
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