Skip to content

Commit

Permalink
Added serialisation to example (#41)
Browse files Browse the repository at this point in the history
* Added serialisation example to notebook

* Added json schema
  • Loading branch information
kbarkhqs authored Nov 13, 2023
1 parent 3f35b88 commit 1eb3dd5
Showing 1 changed file with 279 additions and 24 deletions.
303 changes: 279 additions & 24 deletions qoqo/7_Devices_and_Noise_Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,10 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"id": "a1945017",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GenericDevice { number_qubits: 2, single_qubit_gates: {\"RotateZ\": {0: 1.0, 1: 1.0}}, two_qubit_gates: {\"CNOT\": {(1, 0): 1.0, (0, 1): 1.0}}, multi_qubit_gates: {}, decoherence_rates: {1: [[0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0]], shape=[3, 3], strides=[3, 1], layout=Cc (0x5), const ndim=2, 0: [[0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0]], shape=[3, 3], strides=[3, 1], layout=Cc (0x5), const ndim=2} }\n",
"GenericDevice { number_qubits: 2, single_qubit_gates: {\"RotateZ\": {1: 1.0, 0: 1.0}}, two_qubit_gates: {\"CNOT\": {(0, 1): 1.0, (1, 0): 1.0}}, multi_qubit_gates: {}, decoherence_rates: {0: [[0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0]], shape=[3, 3], strides=[3, 1], layout=Cc (0x5), const ndim=2, 1: [[0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0],\n",
" [0.0, 0.0, 0.0]], shape=[3, 3], strides=[3, 1], layout=Cc (0x5), const ndim=2} }\n"
]
}
],
"outputs": [],
"source": [
"from qoqo import devices\n",
"import numpy as np\n",
Expand Down Expand Up @@ -93,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"id": "capable-dallas",
"metadata": {},
"outputs": [],
Expand All @@ -119,7 +102,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"id": "middle-emerald",
"metadata": {},
"outputs": [],
Expand All @@ -133,6 +116,218 @@
"square_lattice = devices.SquareLatticeDevice(rows, columns, [\"RotateZ\"], [\"CNOT\"], 1.0)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Serialisation\n",
"\n",
"The user can serialise and deserialise the devices using `to_json` and `from_json`. They can also generate the corresponding JSON schema."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n",
" \"title\": \"GenericDevice\",\n",
" \"type\": \"object\",\n",
" \"required\": [\n",
" \"_roqoqo_version\",\n",
" \"decoherence_rates\",\n",
" \"multi_qubit_gates\",\n",
" \"number_qubits\",\n",
" \"single_qubit_gates\",\n",
" \"two_qubit_gates\"\n",
" ],\n",
" \"properties\": {\n",
" \"_roqoqo_version\": {\n",
" \"$ref\": \"#/definitions/RoqoqoVersionSerializable\"\n",
" },\n",
" \"decoherence_rates\": {\n",
" \"description\": \"Decoherence rates for all qubits\",\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"array\",\n",
" \"items\": [\n",
" {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint\",\n",
" \"minimum\": 0.0\n",
" },\n",
" {\n",
" \"$ref\": \"#/definitions/Array2_f64\"\n",
" }\n",
" ],\n",
" \"maxItems\": 2,\n",
" \"minItems\": 2\n",
" }\n",
" },\n",
" \"multi_qubit_gates\": {\n",
" \"description\": \"Gate times for all multi qubit gates\",\n",
" \"type\": \"object\",\n",
" \"additionalProperties\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"array\",\n",
" \"items\": [\n",
" {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint\",\n",
" \"minimum\": 0.0\n",
" }\n",
" },\n",
" {\n",
" \"type\": \"number\",\n",
" \"format\": \"double\"\n",
" }\n",
" ],\n",
" \"maxItems\": 2,\n",
" \"minItems\": 2\n",
" }\n",
" }\n",
" },\n",
" \"number_qubits\": {\n",
" \"description\": \"The number of qubits\",\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint\",\n",
" \"minimum\": 0.0\n",
" },\n",
" \"single_qubit_gates\": {\n",
" \"description\": \"Gate times for all single qubit gates\",\n",
" \"type\": \"object\",\n",
" \"additionalProperties\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"array\",\n",
" \"items\": [\n",
" {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint\",\n",
" \"minimum\": 0.0\n",
" },\n",
" {\n",
" \"type\": \"number\",\n",
" \"format\": \"double\"\n",
" }\n",
" ],\n",
" \"maxItems\": 2,\n",
" \"minItems\": 2\n",
" }\n",
" }\n",
" },\n",
" \"two_qubit_gates\": {\n",
" \"description\": \"Gate times for all two qubit gates\",\n",
" \"type\": \"object\",\n",
" \"additionalProperties\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"array\",\n",
" \"items\": [\n",
" {\n",
" \"type\": \"array\",\n",
" \"items\": [\n",
" {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint\",\n",
" \"minimum\": 0.0\n",
" },\n",
" {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint\",\n",
" \"minimum\": 0.0\n",
" }\n",
" ],\n",
" \"maxItems\": 2,\n",
" \"minItems\": 2\n",
" },\n",
" {\n",
" \"type\": \"number\",\n",
" \"format\": \"double\"\n",
" }\n",
" ],\n",
" \"maxItems\": 2,\n",
" \"minItems\": 2\n",
" }\n",
" }\n",
" }\n",
" },\n",
" \"definitions\": {\n",
" \"Array2_f64\": {\n",
" \"type\": \"object\",\n",
" \"required\": [\n",
" \"data\",\n",
" \"dim\",\n",
" \"v\"\n",
" ],\n",
" \"properties\": {\n",
" \"data\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"number\",\n",
" \"format\": \"double\"\n",
" }\n",
" },\n",
" \"dim\": {\n",
" \"type\": \"array\",\n",
" \"items\": {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint8\",\n",
" \"minimum\": 0.0\n",
" }\n",
" },\n",
" \"v\": {\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint8\",\n",
" \"minimum\": 0.0\n",
" }\n",
" }\n",
" },\n",
" \"RoqoqoVersionSerializable\": {\n",
" \"type\": \"object\",\n",
" \"required\": [\n",
" \"major_version\",\n",
" \"minor_version\"\n",
" ],\n",
" \"properties\": {\n",
" \"major_version\": {\n",
" \"description\": \"The semver major version of roqoqo\",\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint32\",\n",
" \"minimum\": 0.0\n",
" },\n",
" \"minor_version\": {\n",
" \"description\": \"The semver minor version of roqoqo\",\n",
" \"type\": \"integer\",\n",
" \"format\": \"uint32\",\n",
" \"minimum\": 0.0\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}\n"
]
}
],
"source": [
"from qoqo import devices\n",
"\n",
"generic_device = devices.GenericDevice(2)\n",
"device_serialised = generic_device.to_json()\n",
"device_deserialised = devices.GenericDevice.from_json(device_serialised)\n",
"assert generic_device == device_deserialised\n",
"\n",
"print(generic_device.json_schema())"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -153,7 +348,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -183,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -217,7 +412,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -233,6 +428,66 @@
"higher_prob = model.prob_detect_1_as_0(2)\n",
"assert higher_prob == 0.7\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Serialisation\n",
"\n",
"The user can serialise and deserialise the devices using `to_json` and `from_json`. They can also generate the corresponding JSON schema."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n",
" \"title\": \"ImperfectReadoutModel\",\n",
" \"description\": \"Noise model representing readout errors.\\n\\nReadout errors are modeled by two probabilities in this simple model. One probability to detect a 1 instead of a 0 when the quantum measurement gives 0 and one probability to detect a 0 instead of a 1 when the quantum measurement gives 1.\\n\\n# Example\\n\\n```rust use roqoqo::noise_models::ImperfectReadoutModel;\\n\\nlet model = ImperfectReadoutModel::new_with_uniform_error(3, 0.5, 0.5).unwrap(); let model = model.set_error_probabilites(2, 0.3, 0.7).unwrap(); let uniform_prob = model.prob_detect_0_as_1(&0); assert_eq!(uniform_prob, 0.5); let lower_prob = model.prob_detect_0_as_1(&2); assert_eq!(lower_prob, 0.3); let higher_prob = model.prob_detect_1_as_0(&2); assert_eq!(higher_prob, 0.7); ```\",\n",
" \"type\": \"object\",\n",
" \"required\": [\n",
" \"prob_detect_0_as_1\",\n",
" \"prob_detect_1_as_0\"\n",
" ],\n",
" \"properties\": {\n",
" \"prob_detect_0_as_1\": {\n",
" \"description\": \"Decoherence rates for all qubits\",\n",
" \"type\": \"object\",\n",
" \"additionalProperties\": {\n",
" \"type\": \"number\",\n",
" \"format\": \"double\"\n",
" }\n",
" },\n",
" \"prob_detect_1_as_0\": {\n",
" \"type\": \"object\",\n",
" \"additionalProperties\": {\n",
" \"type\": \"number\",\n",
" \"format\": \"double\"\n",
" }\n",
" }\n",
" }\n",
"}\n"
]
}
],
"source": [
"from qoqo import noise_models\n",
"\n",
"model = noise_models.ImperfectReadoutModel.new_with_uniform_error(3, 0.5, 0.5)\n",
"model = model.set_error_probabilites(2, 0.3, 0.7)\n",
"model_serialised = model.to_json()\n",
"model_deserialised = noise_models.ImperfectReadoutModel.from_json(model_serialised)\n",
"assert model == model_deserialised\n",
"\n",
"print(model.json_schema())"
]
}
],
"metadata": {
Expand Down

0 comments on commit 1eb3dd5

Please sign in to comment.