Helper library for encoding classical data into quantum state and vice versa
Project description
Encoder/Decoder
Helper library for encoding classical data into quantum state and vice versa. Primarily built for Quantum Programming Studio.
encode_input(input_data_row, input_encoding)
Function returns OpenQASM 2.0 code which prepares quantum state with input data encoded.
Arguments:
input_data_row dictionary containing single row of data to be encoded into quantum state in a form { "column_name": value, ... }.
Example:
{
"a": 11,
"b": 14
}
(in this example, we have two columns: a with value 11 and b with value 14).
input_encoding dictionary describing encoding scheme and column definitions.
Fields:
-
typeencoding scheme. Currently, only two encoding schemes are implemented:-
basisbasis encoding -
customcustom encoding, which expects you to provide encoding function
-
-
customFunction.pythonused withcustomencoding. Your custom encoding function, which receives the same arguments as thisencode_inputfunction and returns OpenQASM 2.0 string. -
qubitOffsetused bybasisencoding. For example, if input data requires 8 bits to be encoded and qubitOffset is 3, then data will be encoded into qubits [3..10] (from fourth to eleventh qubit). -
colDefslist of column definitions. Column definition is dictionary containing following fields:-
namecolumn name string. Must be valid identifier consisting of letters, numbers or underscores and must start with a letter or underscore. -
structurestring describing structure of a value. Can be one of:scalar,vectorormatrix. -
dimensionsdimension of a vector or matrix. List of integers. Empty if structure isscalar, single integer if structure isvector(number of elements in a vector) and two integers if structure ismatrix(number of rows and number of columns in a matrix). -
typedata type string of a scalar value or data type of the elements of a vector/matrix. Can beintegerorfloat. -
minminimal value. Used by built-inbasisencoder (or you can use it in your custom encoding function). -
maxmaximal value. Used by built-inbasisencoder (or you can use it in your custom encoding function). -
bitsnumber of (classical) bits. Used by built-inbasisencoder: floating point numbers and integers whose range defined by min...max is out of range[0..2**bits]will be quantized to range[0..2**bits]. Or, you can use this field in your custom encoding function as Your Majesty wishes.
-
Example for basis encoding:
from quantastica.encoder_decoder import encode_input
input_encoding = {
"type": "basis",
"qubitOffset": 1,
"colDefs": [
{
"name": "a",
"structure": "scalar",
"dimensions": [],
"type": "integer",
"min": 0,
"max": 15,
"bits": 4
},
{
"name": "b",
"structure": "scalar",
"dimensions": [],
"type": "integer",
"min": 0,
"max": 15,
"bits": 4
}
]
}
input_data_row = { "a": 11, "b": 14 }
qasm = encode_data(input_data_row, input_encoding)
Example will return following OpenQASM 2.0 string:
OPENQASM 2.0;
include "qelib1.inc";
qreg q[9];
x q[1];
x q[2];
x q[4];
x q[6];
x q[7];
x q[8];
Example for custom encoding:
from quantastica.encoder_decoder import encode_input
def custom_encoder(input_data_row, input_encoding):
qasm = ""
qasm += "OPENQASM 2.0;\n"
qasm += "include \"qelib1.inc\";\n"
# ... your code here ..
return qasm
input_encoding = {
"type": "custom",
"customFunction": {
"python": custom_encoder
},
"qubitOffset": 1,
"colDefs": [
{
"name": "a",
"structure": "scalar",
"dimensions": [],
"type": "integer",
"min": 0,
"max": 15,
"bits": 4
},
{
"name": "b",
"structure": "scalar",
"dimensions": [],
"type": "integer",
"min": 0,
"max": 15,
"bits": 4
}
]
}
input_data_row = { "a": 11, "b": 14 }
qasm = encode_data(input_data_row, input_encoding)
decode_output(counts, output_decoding, unpack_values=False)
Function returns output data which is decoded from sampling results of a quantum computer (or simulator).
Arguments:
counts dictionary in a form { "bitstring": occurrences, ... }. For example: { "011010111": 970, "001101001": 30 }.
output_decoding distionary describing encoding scheme and column definitions.
Fields:
-
typedecoding scheme. Currently, only two decoding schemes are implemented:-
basisbasis decoding -
customcustom decoding, which expects you to provide decoding function
-
-
customFunction.pythonused withcustomdecoding. Your custom decoding function, which receives the same arguments as thisdecode_outputfunction and returns data row. -
qubitOffsetused bybasisdecoding. For example, if output data is 8 bits wide and qubitOffset is 3, then data will be decoded from qubits [3..10] (from fourth to eleventh qubit). -
colDefslist of column definitions. Column definition is dictionary containing following fields:-
namecolumn name string. Must be valid identifier consisting of letters, numbers or underscores and must start with a letter or underscore. -
structurestring describing structure of a value. Can be one of:scalar,vectorormatrix. -
dimensionsdimension of a vector or matrix. List of integers. Empty if structure isscalar, single integer if structure isvector(number of elements in a vector) and two integers if structure ismatrix(number of rows and number of columns in a matrix). -
typedata type string of a scalar value or data type of the elements of a vector/matrix. Can beintegerorfloat. -
minminimal value. Used by built-inbasisdecoder (or you can use it in your custom decoding function). -
maxmaximal value. Used by built-inbasisdecoder (or you can use it in your custom decoding function). -
bitsnumber of (classical) bits. Used by built-inbasisdecoder: floating point numbers and integers will be dequantized from range[0..2**bits]to the range defined by min..max. Or, you can use this field in your custom encoding function as you wish.
-
unpack_data boolean. When this argument is False (default), function will return dictionary. For example: { "c": 25 }. If unpack_data is True, the function will simply return only value (or tuple of values if multiple columns are defined).
Example for basis decoding:
from quantastica.encoder_decoder import decode_output
output_decoding = {
"type": "basis",
"qubitOffset": 5,
"colDefs": [
{
"name": "c",
"structure": "scalar",
"dimensions": [],
"type": "integer",
"min": 0,
"max": 31,
"bits": 5
}
]
}
counts = { "1100110110": 1024 } # output from quantum computer or simulator
output_data_row = decode_output(counts, output_decoding)
Example output:
{ "c": 25 }
Example for custom decoding:
from quantastica.encoder_decoder import decode_output
def custom_decoder(counts, output_decoding):
output_data_row = {}
# ... your code here ...
return output_data_row
output_decoding = {
"type": "custom",
"customFunction": {
"python": custom_decoder
},
"qubitOffset": 5,
"colDefs": [
{
"name": "c",
"structure": "scalar",
"dimensions": [],
"type": "integer",
"min": 0,
"max": 31,
"bits": 5
}
]
}
counts = { "1100110110": 1024 } # output from quantum computer or simulator
output_data_row = decode_output(counts, output_decoding)
That's it. Enjoy! :P
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for quantastica-encoder-decoder-0.1.1.tar.gz
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | 387bad2bdfbd5ffabaa44f4bbd0d964826cdc5e0917c1ba6e867def0ac8860ac |
|
| MD5 | 215b1c0b4cf5c2d2e73b0342aa813437 |
|
| BLAKE2b-256 | 3690dd755f21d0e23ccfaae4af76811e5238c21ff27023651fbaa1d798336816 |
Hashes for quantastica_encoder_decoder-0.1.1-py3-none-any.whl
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | da0391bd9ac52be29c167ca4b348131fb13f7370b66081b7ade221600b393da5 |
|
| MD5 | 1851a06cface4627f3f593341e593c36 |
|
| BLAKE2b-256 | 05fc357e892fddd833ec7daa0a2669d5bacc0bff80d1e5c0c5cab6af887753f9 |