Last active
October 31, 2024 04:29
-
-
Save FuzzysTodd/a3c769f32ac9d7c0ef25548dc2a551c5 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.27+commit.40a35a09.js&optimize=true&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "722f92e3-4324-41b8-bebb-eb5c5c1c0f1a", | |
"metadata": {}, | |
"source": [ | |
"# Quantum Counting\n", | |
"\n", | |
"Here we count the number of solutions to the equation $x+y\\leq 7$ where x, y are integers of size `REG_SIZE` bits. " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "3226860d-953f-4993-82fd-f629497d58c7", | |
"metadata": {}, | |
"source": [ | |
"## Using the `AmplitudeEstimation` function" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "0db8dd49-550a-419e-bce1-1a19a4a88ba7", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Opening: https://platform.classiq.io/circuit/388ff783-1868-4e26-b930-03b6eb01fd35?version=0.28.0\n" | |
] | |
} | |
], | |
"source": [ | |
"from classiq import Model, Constraints, synthesize, show, execute\n", | |
"from classiq.builtin_functions import GroverOperator, ArithmeticOracle, AmplitudeEstimation\n", | |
"from classiq import RegisterUserInput\n", | |
"from classiq.execution import ExecutionPreferences\n", | |
"from classiq.execution import QaeWithQpeEstimationMethod\n", | |
"\n", | |
"REG_SIZE=3\n", | |
"NUM_PHASE_QUBITS=3\n", | |
"\n", | |
"arith_params = ArithmeticOracle(\n", | |
" expression = \"x + y <= 7\",\n", | |
" definitions=dict(\n", | |
" x = RegisterUserInput(size=REG_SIZE),\n", | |
" y = RegisterUserInput(size=REG_SIZE)),\n", | |
")\n", | |
"\n", | |
"grover_operator_params = GroverOperator(\n", | |
" oracle_params = arith_params,\n", | |
")\n", | |
"\n", | |
"ae_params = AmplitudeEstimation(\n", | |
" estimation_register_size=NUM_PHASE_QUBITS, grover_operator=grover_operator_params\n", | |
")\n", | |
"\n", | |
"model = Model()\n", | |
"\n", | |
"model.constraints = Constraints(max_width=12)\n", | |
"\n", | |
"qae_out = model.AmplitudeEstimation(ae_params)\n", | |
"\n", | |
"# necessary for the post-process to recognize the register\n", | |
"model.set_outputs({'ESTIMATED_AMPLITUDE_OUTPUT': qae_out['ESTIMATED_AMPLITUDE_OUTPUT']})\n", | |
"\n", | |
"# set execution instructions\n", | |
"model.sample()\n", | |
"model.post_process_amplitude_estimation(estimation_register_size=NUM_PHASE_QUBITS, estimation_method=QaeWithQpeEstimationMethod.MAXIMUM_LIKELIHOOD)\n", | |
"\n", | |
"quantum_program = synthesize(model.get_model())\n", | |
"show(quantum_program)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "21a9aef8-996d-4b92-92f7-616412985fb9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The probability estimation of the good states is: 0.14644660940672624\n" | |
] | |
} | |
], | |
"source": [ | |
"qae_result = execute(quantum_program)\n", | |
"print(f\"The probability estimation of the good states is: {qae_result[1].value}\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "93064d5d-25ba-4fd5-b3fd-eef55b7e07bd", | |
"metadata": {}, | |
"source": [ | |
"## Explicitly defining the amplitude estimation circuit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"id": "786636e3-91f8-42d8-8218-9caa5ac142ec", | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Opening: https://platform.classiq.io/circuit/415ca5ff-63d5-485a-af16-1afaa6549cd7?version=0.28.0\n" | |
] | |
} | |
], | |
"source": [ | |
"from classiq import Model, Constraints, synthesize, show, execute\n", | |
"from classiq.builtin_functions import GroverOperator, ArithmeticOracle, UniformDistributionStatePreparation, PhaseEstimation\n", | |
"from classiq import RegisterUserInput\n", | |
"from classiq.execution import ExecutionPreferences\n", | |
"from classiq.execution import QaeWithQpeEstimationMethod\n", | |
"\n", | |
"REG_SIZE=3\n", | |
"NUM_PHASE_QUBITS=3\n", | |
"\n", | |
"sp_params = UniformDistributionStatePreparation(num_qubits=REG_SIZE)\n", | |
"arith_params = ArithmeticOracle(\n", | |
" expression = \"x + y <= 7\",\n", | |
" definitions=dict(\n", | |
" x = RegisterUserInput(size=REG_SIZE),\n", | |
" y = RegisterUserInput(size=REG_SIZE)),\n", | |
")\n", | |
"\n", | |
"grover_operator_params = GroverOperator(\n", | |
" oracle_params = arith_params,\n", | |
")\n", | |
"\n", | |
"qpe_params = PhaseEstimation(\n", | |
" size=NUM_PHASE_QUBITS, unitary=\"GroverOperator\", unitary_params=grover_operator_params\n", | |
")\n", | |
"\n", | |
"model = Model()\n", | |
"model.constraints = Constraints(max_width=12)\n", | |
"\n", | |
"sp_x = model.UniformDistributionStatePreparation(sp_params)['OUT']\n", | |
"sp_y = model.UniformDistributionStatePreparation(sp_params)['OUT']\n", | |
"\n", | |
"qpe_out = model.PhaseEstimation(params=qpe_params, in_wires={'x': sp_x, 'y': sp_y})\n", | |
"\n", | |
"# necessary for the post-process to recognize the register\n", | |
"model.set_outputs({'ESTIMATED_AMPLITUDE_OUTPUT': qpe_out['PHASE_ESTIMATION']})\n", | |
"\n", | |
"# set execution instructions\n", | |
"model.sample()\n", | |
"model.post_process_amplitude_estimation(estimation_register_size=NUM_PHASE_QUBITS, estimation_method=QaeWithQpeEstimationMethod.MAXIMUM_LIKELIHOOD)\n", | |
"\n", | |
"quantum_program = synthesize(model.get_model())\n", | |
"show(quantum_program)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "78f3ccab-1b22-42d5-a328-6ed44abcd88b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The probability estimation of the good states is: 0.5000000000000001\n" | |
] | |
} | |
], | |
"source": [ | |
"qae_result = execute(quantum_program)\n", | |
"print(f\"The probability estimation of the good states is: {qae_result[1].value}\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
}{ | |
"deploy": { | |
"VM:-": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"main:1": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"ropsten:3": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"rinkeby:4": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"kovan:42": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"goerli:5": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
}, | |
"Custom": { | |
"linkReferences": {}, | |
"autoDeployLib": true | |
} | |
}, | |
"data": { | |
"bytecode": { | |
"linkReferences": {}, | |
"object": "", | |
"opcodes": "", | |
"sourceMap": "" | |
}, | |
"deployedBytecode": { | |
"immutableReferences": {}, | |
"linkReferences": {}, | |
"object": "", | |
"opcodes": "", | |
"sourceMap": "" | |
}, | |
"gasEstimates": null, | |
"methodIdentifiers": { | |
"deposit(bytes,bytes,bytes,bytes32)": "22895118", | |
"get_deposit_count()": "621fd130", | |
"get_deposit_root()": "c5f2892f" | |
} | |
}, | |
"abi": [ | |
{ | |
"anonymous": false, | |
"inputs": [ | |
{ | |
"indexed": false, | |
"internalType": "bytes", | |
"name": "pubkey", | |
"type": "bytes" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "bytes", | |
"name": "withdrawal_credentials", | |
"type": "bytes" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "bytes", | |
"name": "amount", | |
"type": "bytes" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "bytes", | |
"name": "signature", | |
"type": "bytes" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "bytes", | |
"name": "index", | |
"type": "bytes" | |
} | |
], | |
"name": "DepositEvent", | |
"type": "event" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"internalType": "bytes", | |
"name": "pubkey", | |
"type": "bytes" | |
}, | |
{ | |
"internalType": "bytes", | |
"name": "withdrawal_credentials", | |
"type": "bytes" | |
}, | |
{ | |
"internalType": "bytes", | |
"name": "signature", | |
"type": "bytes" | |
}, | |
{ | |
"internalType": "bytes32", | |
"name": "deposit_data_root", | |
"type": "bytes32" | |
} | |
], | |
"name": "deposit", | |
"outputs": [], | |
"stateMutability": "payable", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "get_deposit_count", | |
"outputs": [ | |
{ | |
"internalType": "bytes", | |
"name": "", | |
"type": "bytes" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
}, | |
{ | |
"inputs": [], | |
"name": "get_deposit_root", | |
"outputs": [ | |
{ | |
"internalType": "bytes32", | |
"name": "", | |
"type": "bytes32" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment