diff --git a/quantumcat/applications/generator/__init__.py b/quantumcat/applications/generator/__init__.py index d40869a..dcc9229 100644 --- a/quantumcat/applications/generator/__init__.py +++ b/quantumcat/applications/generator/__init__.py @@ -14,3 +14,5 @@ from quantumcat.applications.generator.random_number import RandomNumber +from quantumcat.applications.generator.password import Password +from quantumcat.applications.generator.otp import OTP \ No newline at end of file diff --git a/quantumcat/applications/generator/otp.py b/quantumcat/applications/generator/otp.py new file mode 100644 index 0000000..74609b3 --- /dev/null +++ b/quantumcat/applications/generator/otp.py @@ -0,0 +1,35 @@ +# (C) Copyright Artificial Brain 2021. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import requests +from quantumcat.utils import constants, ErrorMessages +from quantumcat.exceptions import OTPLengthError + + +class OTP: + def __init__(self): + self.length = 5 + + def generate(self): + ANU_QRNG_REQUEST_URL = constants.ANU_QRNG_URL + '?length=10&type=' + \ + constants.UNIT16_TYPE + response = requests.get(ANU_QRNG_REQUEST_URL) + if response.status_code != 200: + return 'Something went wromg' + data = response.json() + otp = data['data'] + for i in range(len(otp)): + if len(str(otp[i])) == self.length: + return otp[i] diff --git a/quantumcat/applications/generator/password.py b/quantumcat/applications/generator/password.py new file mode 100644 index 0000000..c2a499d --- /dev/null +++ b/quantumcat/applications/generator/password.py @@ -0,0 +1,36 @@ +# (C) Copyright Artificial Brain 2021. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import requests +from quantumcat.utils import constants, ErrorMessages +from quantumcat.exceptions import PasswordLengthError + + +class Password: + def __init__(self, length): + self.length = length + + def generate(self): + if not (4 < self.length <= 20): + raise PasswordLengthError(ErrorMessages.PASSWORD_LENGTH_INCORRECT) + + ANU_QRNG_REQUEST_URL = constants.ANU_QRNG_URL + '?length=1&type=' + \ + constants.HEX_TYPE + '&size=' + str(self.length) + response = requests.get(ANU_QRNG_REQUEST_URL) + if response.status_code != 200: + return 'Something went wromg' + data = response.json() + password = data['data'][0] + return password[0: self.length] diff --git a/quantumcat/circuit/circuit.py b/quantumcat/circuit/circuit.py index 171f9dc..2b77e78 100644 --- a/quantumcat/circuit/circuit.py +++ b/quantumcat/circuit/circuit.py @@ -382,7 +382,7 @@ def get_operations(self): def check_qubit_boundary(self, qubit): if qubit > (self.qubits - 1): - raise CircuitError(ErrorMessages.qubit_out_of_bound) + raise CircuitError(ErrorMessages.QUBIT_OUT_OF_BOUND) def draw_circuit(self, provider=providers.DEFAULT_PROVIDER): self.check_and_convert(provider) @@ -427,7 +427,7 @@ def execute(self, provider=providers.DEFAULT_PROVIDER, simulator_name, repetitions, api) elif self.provider == providers.IONQ_PROVIDER: if api is None: - raise APIDetailsNotFoundError(ErrorMessages.ionq_api_details_not_provided) + raise APIDetailsNotFoundError(ErrorMessages.IONQ_API_DETAILS_NOT_PROVIDED) return execute_circuit.on_ionq(self.converted_q_circuit, default_target, repetitions, api, self.get_operations()) diff --git a/quantumcat/exceptions/__init__.py b/quantumcat/exceptions/__init__.py index eab8b02..63c3df8 100644 --- a/quantumcat/exceptions/__init__.py +++ b/quantumcat/exceptions/__init__.py @@ -12,7 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. + from quantumcat.exceptions.exception import QuantumCatError from quantumcat.exceptions.circuit_error import CircuitError from quantumcat.exceptions.install_error import InstallNotFoundError from quantumcat.exceptions.api_error import APIDetailsNotFoundError +from quantumcat.exceptions.password_length import PasswordLengthError +from quantumcat.exceptions.otp_length import OTPLengthError diff --git a/quantumcat/exceptions/otp_length.py b/quantumcat/exceptions/otp_length.py new file mode 100644 index 0000000..0be5da0 --- /dev/null +++ b/quantumcat/exceptions/otp_length.py @@ -0,0 +1,21 @@ +# (C) Copyright Artificial Brain 2021. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from quantumcat.exceptions import QuantumCatError + + +class OTPLengthError(QuantumCatError): + """Base class for errors raised for api details not found.""" + + pass diff --git a/quantumcat/exceptions/password_length.py b/quantumcat/exceptions/password_length.py new file mode 100644 index 0000000..124e166 --- /dev/null +++ b/quantumcat/exceptions/password_length.py @@ -0,0 +1,21 @@ +# (C) Copyright Artificial Brain 2021. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from quantumcat.exceptions import QuantumCatError + + +class PasswordLengthError(QuantumCatError): + """Base class for errors raised for api details not found.""" + + pass diff --git a/quantumcat/utils/constants.py b/quantumcat/utils/constants.py index 59ba922..dc5a711 100644 --- a/quantumcat/utils/constants.py +++ b/quantumcat/utils/constants.py @@ -19,3 +19,6 @@ PARAMS = 'params' BINARY = 'binary' DECIMAL = 'decimal' +ANU_QRNG_URL = 'https://qrng.anu.edu.au/API/jsonI.php' +HEX_TYPE = 'hex16' +UNIT16_TYPE = 'uint16' diff --git a/quantumcat/utils/error_messages.py b/quantumcat/utils/error_messages.py index f92829e..390d177 100644 --- a/quantumcat/utils/error_messages.py +++ b/quantumcat/utils/error_messages.py @@ -12,8 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -qubit_out_of_bound = 'Qubit is out of bound.' -cbit_out_of_bound = 'Classical Bit is out of bound.' -qiskit_not_installed = 'Qiskit is not installed.' -api_details_not_provided = 'API Key is required for running on real quantum device.' -ionq_api_details_not_provided = 'API Key is required for running on IonQ.' +QUBIT_OUT_OF_BOUND = 'Qubit is out of bound.' +CBIT_OUT_OF_BOUND = 'Classical Bit is out of bound.' +QISKIT_NOT_INSTALLED = 'Qiskit is not installed.' +API_DETAILS_NOT_PROVIDED = 'API Key is required for running on real quantum device.' +IONQ_API_DETAILS_NOT_PROVIDED = 'API Key is required for running on IonQ.' +PASSWORD_LENGTH_INCORRECT = 'Password length should be between 5 and 20' +OTP_LENGTH_INCORRECT = 'OTP length should be either 4 or 5' + diff --git a/requirements.txt b/requirements.txt index 479a72c..3c762a5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ qiskit==0.26.2 cirq==0.11.0 amazon-braket-sdk==1.6.4 +requests==2.25.1