DQCsim
|
Plugin definition class. More...
Public Member Functions | |
Plugin (HandleIndex handle) noexcept | |
Wraps the given plugin definition handle. More... | |
Plugin (PluginType type, const std::string &name, const std::string &author, const std::string &version) | |
Constructs a new plugin definition object. More... | |
Plugin (const Plugin &)=delete | |
void | operator= (const Plugin &)=delete |
Plugin (Plugin &&)=default | |
Default move constructor. | |
Plugin & | operator= (Plugin &&)=default |
Default move assignment. | |
PluginType | get_type () const |
Returns the plugin type described by this object. More... | |
std::string | get_name () const |
Returns the name of the described plugin. More... | |
std::string | get_author () const |
Returns the author of the described plugin. More... | |
std::string | get_version () const |
Returns the version of the described plugin. More... | |
Plugin && | with_initialize (const callback::Initialize &cb) |
Assigns the initialize callback function from a pre-existing callback::Initialize object by copy. More... | |
Plugin && | with_initialize (callback::Initialize &&cb) |
Assigns the initialize callback function from a pre-existing callback::Initialize object by move. More... | |
template<typename... Args> | |
Plugin && | with_initialize (Args... args) |
Assigns the initialize callback function by constructing the callback object implicitly. More... | |
Plugin && | with_drop (const callback::Drop &cb) |
Assigns the drop callback function from a pre-existing callback::Drop object by copy. More... | |
Plugin && | with_drop (callback::Drop &&cb) |
Assigns the drop callback function from a pre-existing callback::Drop object by move. More... | |
template<typename... Args> | |
Plugin && | with_drop (Args... args) |
Assigns the drop callback function by constructing the callback object implicitly. More... | |
Plugin && | with_run (const callback::Run &cb) |
Assigns the run callback function from a pre-existing callback::Run object by copy. More... | |
Plugin && | with_run (callback::Run &&cb) |
Assigns the run callback function from a pre-existing callback::Run object by move. More... | |
template<typename... Args> | |
Plugin && | with_run (Args... args) |
Assigns the run callback function by constructing the callback object implicitly. More... | |
Plugin && | with_allocate (const callback::Allocate &cb) |
Assigns the allocate callback function from a pre-existing callback::Allocate object by copy. More... | |
Plugin && | with_allocate (callback::Allocate &&cb) |
Assigns the allocate callback function from a pre-existing callback::Allocate object by move. More... | |
template<typename... Args> | |
Plugin && | with_allocate (Args... args) |
Assigns the allocate callback function by constructing the callback object implicitly. More... | |
Plugin && | with_free (const callback::Free &cb) |
Assigns the free callback function from a pre-existing callback::Free object by copy. More... | |
Plugin && | with_free (callback::Free &&cb) |
Assigns the free callback function from a pre-existing callback::Free object by move. More... | |
template<typename... Args> | |
Plugin && | with_free (Args... args) |
Assigns the free callback function by constructing the callback object implicitly. More... | |
Plugin && | with_gate (const callback::Gate &cb) |
Assigns the gate callback function from a pre-existing callback::Gate object by copy. More... | |
Plugin && | with_gate (callback::Gate &&cb) |
Assigns the gate callback function from a pre-existing callback::Gate object by move. More... | |
template<typename... Args> | |
Plugin && | with_gate (Args... args) |
Assigns the gate callback function by constructing the callback object implicitly. More... | |
Plugin && | with_modify_measurement (const callback::ModifyMeasurement &cb) |
Assigns the modify-measurement callback function from a pre-existing callback::ModifyMeasurement object by copy. More... | |
Plugin && | with_modify_measurement (callback::ModifyMeasurement &&cb) |
Assigns the modify-measurement callback function from a pre-existing callback::ModifyMeasurement object by move. More... | |
template<typename... Args> | |
Plugin && | with_modify_measurement (Args... args) |
Assigns the modify-measurement callback function by constructing the callback object implicitly. More... | |
Plugin && | with_advance (const callback::Advance &cb) |
Assigns the advance callback function from a pre-existing callback::Advance object by copy. More... | |
Plugin && | with_advance (callback::Advance &&cb) |
Assigns the advance callback function from a pre-existing callback::Advance object by move. More... | |
template<typename... Args> | |
Plugin && | with_advance (Args... args) |
Assigns the advance callback function by constructing the callback object implicitly. More... | |
Plugin && | with_upstream_arb (const callback::Arb &cb) |
Assigns the upstream-arb callback function from a pre-existing callback::Arb object by copy. More... | |
Plugin && | with_upstream_arb (callback::Arb &&cb) |
Assigns the upstream-arb callback function from a pre-existing callback::Arb object by move. More... | |
template<typename... Args> | |
Plugin && | with_upstream_arb (Args... args) |
Assigns the upstream-arb callback function by constructing the callback object implicitly. More... | |
Plugin && | with_host_arb (const callback::Arb &cb) |
Assigns the host-arb callback function from a pre-existing callback::Arb object by copy. More... | |
Plugin && | with_host_arb (callback::Arb &&cb) |
Assigns the host-arb callback function from a pre-existing callback::Arb object by move. More... | |
template<typename... Args> | |
Plugin && | with_host_arb (Args... args) |
Assigns the host-arb callback function by constructing the callback object implicitly. More... | |
void | run (const char *simulator) |
Runs the defined plugin in the current thread with the given simulator connection descriptor string. More... | |
int | run (int argc, char *argv[]) noexcept |
Runs the defined plugin in the current thread with the given command line. More... | |
PluginJoinHandle | start (const char *simulator) |
Starts the defined plugin in the current thread. More... | |
Public Member Functions inherited from dqcsim::wrap::Handle | |
Handle () noexcept | |
Constructs an empty wrapper. More... | |
Handle (HandleIndex handle) noexcept | |
Wraps the given raw handle. More... | |
virtual | ~Handle () noexcept |
Delete the handle and its wrapper. | |
void | free () |
Explicitly delete the handle, allowing errors to be caught. More... | |
bool | is_valid () const noexcept |
Returns whether this wrapper (still) contains a valid handle. More... | |
HandleIndex | get_handle () const noexcept |
Returns the raw handle without relinquishing ownership. More... | |
HandleIndex | take_handle () noexcept |
Returns the raw handle and relinquishes ownership. More... | |
Handle (const Handle &)=delete | |
void | operator= (const Handle &)=delete |
Handle (Handle &&src) | |
Move constructor; simply moves ownership of the handle from the source object to the constructed object. More... | |
Handle & | operator= (Handle &&src) |
Move constructor; simply moves ownership of the handle from the source object to the assignment target. More... | |
std::string | dump () const |
Returns a string containing a debug dump of the handle. More... | |
HandleType | type () const |
Returns the type of this handle. More... | |
Static Public Member Functions | |
static Plugin | Frontend (const std::string &name, const std::string &author, const std::string &version) |
Shorthand for constructing a new frontend plugin. More... | |
static Plugin | Operator (const std::string &name, const std::string &author, const std::string &version) |
Shorthand for constructing a new operator plugin. More... | |
static Plugin | Backend (const std::string &name, const std::string &author, const std::string &version) |
Shorthand for constructing a new backend plugin. More... | |
Additional Inherited Members | |
Protected Attributes inherited from dqcsim::wrap::Handle | |
HandleIndex | handle |
The wrapped handle. | |
Plugin definition class.
To make a DQCsim plugin in C++, you must construct one of these objects, assign callback functions that define your plugin's functionality, and then call run
or start
. Having done so, you can point the dqcsim
command line or a host program to your plugin's executable.
DQCsim supports three kinds of plugins: frontends, operators, and backends:
Each plugin type supports a different set of callback functions to define the plugin's functionality, but many of the callback functions are shared. The following matrix shows which functions are required (x), optional (o), and not applicable (-):
Callback | Frontend | Operator | Backend |
---|---|---|---|
initialize | o | o | o |
drop | o | o | o |
run | x | - | - |
allocate | - | o | o |
free | - | o | o |
gate | - | o | x |
modify_measurement | - | o | - |
advance | - | o | o |
upstream_arb | - | o | o |
host_arb | o | o | o |
These callbacks perform the following functions.
The initialize callback is to be used by plugins to initialize their internal state, if any. All plugin types support it. It is always called before any of the other callbacks are run, but after the downstream plugins have been initialized. That means it's legal to execute downstream commands here already, such as pre-allocating a number of qubits, preparing their state, querying the capabilities of downstream plugins through ArbCmd
s, etc.
Besides the common arguments, the callback receives a queue of ArbCmd
s containing user-defined initialization commands. These can be thought of as parameters or command-line arguments affecting the behavior of the plugin. Note that plugins should not use the actual command line arguments for their own purposes, as they cannot be set with DQCsim. Rather, the command-line arguments of the plugin are used by DQCsim to pass a connection endpoint string used to let the plugin connect to DQCsim's main process. There is one exception to this baked into DQCsim intended to make running quantum algorithm files more intuitive: if such a script is passed to DQCsim, DQCsim will search for a plugin based on the file extension of the script, and pass the filename of the script to the plugin using the second command-line argument. The search algorithm is described in DQCsim's command-line help, among other places.
If this callback is not supplied, the default behavior is no-op.
The drop callback can be used to do the inverse operation of the initialize callback. It is called when a plugin is gracefully terminated, and is supported by all plugin types. Note that graceful termination does not mean there was no error; it just means that things didn't crash hard enough for DQCsim to lose the ability to send the drop command.
It is not recommended to execute any downstream instructions at this time, as any errors caused at this time will crash DQCsim. However, it will work in case this is really necessary.
If this callback is not supplied, the default behavior is no-op.
This is the primary callback for frontend plugins, in which all the magic should happen. It must therefore be defined. The other plugin types don't support it.
It is called in response to a start()
host API call. It can therefore be called multiple times, though for most simulations it's only called once. The start()
function takes an ArbData
object, which is passed to the callback as an argument. The return value is also an ArbData
, which is returned to the host through its wait()
API call.
The allocate callback is called when the upstream plugin requests that a number of qubits be allocated. DQCsim will allocate the qubit indices automatically, which are passed to the function in the form of a QubitSet
, but of course it is up to the plugin to allocate its own data structures to track the state of the qubits.
The upstream plugin can also specify an ArbCmdQueue
to supply additional information for the qubit, for instance to request that a specific error model be used. Plugins are free to ignore this, as per the semantics of the ArbCmd
interface and operation identifiers.
The default behavior for operator plugins is to pass the allocation command on to the backend without modification. For backends it is no-op. Frontends do not support this callback.
The free callback is called when the upstream plugin requests that a number of qubits be freed. After this call, DQCsim will ensure that the qubit indices are never used again. Plugins may use this to collapse any remaining superposition state in a random way, free up memory, or reuse the memory already allocated for these qubits for new qubits allocated later.
The default behavior for operator plugins is to pass the command on to the backend without modification. For backends it is no-op. Frontends do not support this callback.
The gate callback is called when the upstream plugin requests that a gate be executed. The semantics of this are documented in in the Gate
class.
Backend plugins must return a measurement result set containing exactly those qubits specified in the measurement set. For operators, however, the story is more complicated. Let's say we want to make a silly operator that inverts all measurements. The trivial way to do this would be to forward the gate, query all the measurement results using PluginState::get_measurement()
, invert them, stick them in a measurement result set, and return that result set. However, this approach would not be very efficient, because PluginState::get_measurement()
has to wait for all downstream plugins to finish executing the gate, forcing the OS to switch threads, etc. Instead, operators are allowed to return only a subset (or none) of the measured qubits, as long as they return exactly the remaining measurements as they arrive through the modify-measurement callback.
The default implementation for this callback for operators is to pass the gate through to the downstream plugin and return an empty set of measurements. Combined with the default implementation of modify-measurement, this behavior is correct. Backends must virtual this callback, and frontends do not support it.
Note that for our silly example operator, the default behavior for this function is actually sufficient; you'd only have to virtual the modify-measurement callback in that case.
This callback is called for every measurement result received from the downstream plugin, and returns the measurements that should be reported to the upstream plugin.
Note that while this function is called for only a single measurement at a time, it is allowed to produce a set of measurements. This allows you to cancel propagation of the measurement by returning an empty vector, to just modify the measurement data itself, or to generate additional measurements from a single measurement. However, if you need to modify the qubit references for operators that remap qubits, take care to only send measurement data upstream when these were explicitly requested through the associated upstream gate function's measured list. It is up to you to keep the stream of measurements returned upstream consistent with the gates it sent to your plugin.
PluginState::get_measurement()
and friends are consistent with the results received from downstream. That is, they are not affected by this function. Only the measurement retrieval functions in the upstream plugin are.This callback is called when the upstream plugin calls advance
to advance the simulation time by a number of cycles. Operators and backends can use this to update their error models. The default behavior for an operator is to pass the request on to the downstream plugin, while the default for backends is to ignore it. Frontends don't support it.
This callback is called when the upstream plugin calls arb
. It receives the ArbCmd
as an argument, and must return an ArbData
, which is in turn returned through the upstream plugin's arb
function.
The default behavior for operators is to forward the command to the downstream plugin. Even if you virtual this callback, you should maintain this behavior for any interface identifiers unknown to you. The default for backends is to ignore it. Frontends don't support it.
This callback is called when the host sends an ArbCmd
to the plugin. It receives the ArbCmd
as an argument, and must return an ArbData
, which is in turn returned to the host.
All plugins support this callback. The default behavior is no-op.
|
inlinenoexcept |
|
inline |
Constructs a new plugin definition object.
type | The type of the plugin being defined. |
name | Name with which the plugin class can be identified, not to be confused with the instance name later. |
author | Name of the plugin author. |
version | Version information for the plugin. |
std::runtime_error | When plugin definition handle construction fails. |
|
inlinestatic |
Shorthand for constructing a new frontend plugin.
name | Name with which the plugin class can be identified, not to be confused with the instance name later. |
author | Name of the plugin author. |
version | Version information for the plugin. |
std::runtime_error | When plugin definition handle construction fails. |
|
inlinestatic |
Shorthand for constructing a new operator plugin.
name | Name with which the plugin class can be identified, not to be confused with the instance name later. |
author | Name of the plugin author. |
version | Version information for the plugin. |
std::runtime_error | When plugin definition handle construction fails. |
|
inlinestatic |
Shorthand for constructing a new backend plugin.
name | Name with which the plugin class can be identified, not to be confused with the instance name later. |
author | Name of the plugin author. |
version | Version information for the plugin. |
std::runtime_error | When plugin definition handle construction fails. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Assigns the initialize callback function from a pre-existing callback::Initialize
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the initialize callback function from a pre-existing callback::Initialize
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the initialize callback function by constructing the callback object implicitly.
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the drop callback function from a pre-existing callback::Drop
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the drop callback function from a pre-existing callback::Drop
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
|
inline |
Assigns the run callback function from a pre-existing callback::Run
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the run callback function from a pre-existing callback::Run
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
|
inline |
Assigns the allocate callback function from a pre-existing callback::Allocate
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the allocate callback function from a pre-existing callback::Allocate
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the allocate callback function by constructing the callback object implicitly.
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the free callback function from a pre-existing callback::Free
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the free callback function from a pre-existing callback::Free
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
|
inline |
Assigns the gate callback function from a pre-existing callback::Gate
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the gate callback function from a pre-existing callback::Gate
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
|
inline |
Assigns the modify-measurement callback function from a pre-existing callback::ModifyMeasurement
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the modify-measurement callback function from a pre-existing callback::ModifyMeasurement
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the modify-measurement callback function by constructing the callback object implicitly.
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the advance callback function from a pre-existing callback::Advance
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the advance callback function from a pre-existing callback::Advance
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
|
inline |
Assigns the upstream-arb callback function from a pre-existing callback::Arb
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the upstream-arb callback function from a pre-existing callback::Arb
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the upstream-arb callback function by constructing the callback object implicitly.
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the host-arb callback function from a pre-existing callback::Arb
object by copy.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the host-arb callback function from a pre-existing callback::Arb
object by move.
cb | The callback object. |
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Assigns the host-arb callback function by constructing the callback object implicitly.
&self
, to continue building. std::runtime_error | When the current handle is invalid or of an unsupported plugin type, or when the callback object is invalid. |
|
inline |
Runs the defined plugin in the current thread with the given simulator connection descriptor string.
simulator | Simulator connection descriptor. This should come from the first (for normal plugins) or second (for script-interpreting plugins) command-line argument of the plugin executable (in the latter case, the first argument is the script filename). |
std::runtime_error | When plugin execution fails. |
|
inlinenoexcept |
Runs the defined plugin in the current thread with the given command line.
This is normally the end of the only statement in your plugin executable's main()
.
argc | The argc parameter from your plugin executable's main() . |
argv | The argv parameter from your plugin executable's main() . |
|
inline |
Starts the defined plugin in the current thread.
simulator | Simulator connection descriptor. This should come from the first (for normal plugins) or second (for script-interpreting plugins) command-line argument of the plugin executable (in the latter case, the first argument is the script filename). |
PluginJoinHandle
object that allows the plugin to be waited on. std::runtime_error | When the plugin could not be started. |