From 936a1ee2b473448be1782f808c24bc8cc99ab91c Mon Sep 17 00:00:00 2001 From: mbeisel Date: Thu, 11 Jan 2024 18:31:59 +0100 Subject: [PATCH] formatting --- .../algorithms/algorithm_controller.py | 37 +++++++++---------- .../encoding/encoding_controller.py | 26 ++++++++++--- app/model/algorithm_request.py | 32 +++++++++++++--- app/model/circuit_response.py | 7 +++- app/model/encoding_request.py | 4 +- app/services/algorithm_service.py | 14 ++++++- app/services/encoding_service.py | 7 +++- 7 files changed, 91 insertions(+), 36 deletions(-) diff --git a/app/controller/algorithms/algorithm_controller.py b/app/controller/algorithms/algorithm_controller.py index 12e8ec4..cf44269 100644 --- a/app/controller/algorithms/algorithm_controller.py +++ b/app/controller/algorithms/algorithm_controller.py @@ -47,7 +47,9 @@ @blp.route("/hhl", methods=["POST"]) @blp.arguments( HHLAlgorithmRequestSchema, - example=dict(matrix=[[1.5, 0.5], [0.5, 1.5]], vector=[0, 1], circuit_format="openqasm2"), + example=dict( + matrix=[[1.5, 0.5], [0.5, 1.5]], vector=[0, 1], circuit_format="openqasm2" + ), ) @blp.response(200, HHLResponseSchema) def encoding(json: HHLAlgorithmRequest): @@ -63,7 +65,7 @@ def encoding(json: HHLAlgorithmRequest): reps=2, gammas=[1.0, 1.2], betas=[0.4, 0.7], - circuit_format="openqasm2" + circuit_format="openqasm2", ), ) @blp.response(200, QAOAResponseSchema) @@ -75,12 +77,7 @@ def encoding(json: QAOAAlgorithmRequest): @blp.route("/qft", methods=["POST"]) @blp.arguments( QFTAlgorithmRequestSchema, - example=dict( - n_qubits=4, - inverse=False, - barriers=True, - circuit_format="openqasm2" - ), + example=dict(n_qubits=4, inverse=False, barriers=True, circuit_format="openqasm2"), ) @blp.response(200, QFTResponseSchema) def encoding(json: QFTAlgorithmRequest): @@ -94,7 +91,7 @@ def encoding(json: QFTAlgorithmRequest): example=dict( n_eval_qubits=3, unitary='OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[1];\np(pi/2) q[0];\n', - circuit_format="openqasm2" + circuit_format="openqasm2", ), ) @blp.response(200, QPEResponseSchema) @@ -109,7 +106,7 @@ def encoding(json: QPEAlgorithmRequest): example=dict( parameters=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], observable="Z^Y", - circuit_format="openqasm2" + circuit_format="openqasm2", ), ) @blp.response(200, VQEResponseSchema) @@ -127,7 +124,7 @@ def encoding(json: VQEAlgorithmRequest): iterations=2, reflection_qubits=[0, 1], barriers=True, - circuit_format="openqasm2" + circuit_format="openqasm2", ), ) @blp.response(200, GroverResponseSchema) @@ -144,14 +141,16 @@ def encoding(json: GroverAlgorithmRequest): p=2, betas=[1.0, 2.0], gammas=[1.0, 3.0], - circuit_format="openqasm2" + circuit_format="openqasm2", ), description="Currently, only 3x3 and 4x4 matrices supported.", ) @blp.response(200, CircuitResponseSchema) def encoding(json): if json: - return algorithm_service.generate_tsp_qaoa_circuit(TSPQAOAAlgorithmRequest(**json)) + return algorithm_service.generate_tsp_qaoa_circuit( + TSPQAOAAlgorithmRequest(**json) + ) @blp.route("/maxcutqaoa", methods=["POST"]) @@ -164,7 +163,7 @@ def encoding(json): gammas=[1.0], p=1, parameterized=False, - circuit_format="openqasm2" + circuit_format="openqasm2", ), ) @blp.response(200, CircuitResponseSchema) @@ -189,21 +188,21 @@ def get_maxcut_circuit(json: dict): p=1, betas=[1.0], gammas=[1.0], - circuit_format="openqasm2" + circuit_format="openqasm2", ), ) @blp.response(200, CircuitResponseSchema) def get_knapsack_circuit(json: KnapsackQAOAAlgorithmRequest): if json: - return algorithm_service.generate_knapsack_qaoa_circuit(KnapsackQAOAAlgorithmRequest(**json)) + return algorithm_service.generate_knapsack_qaoa_circuit( + KnapsackQAOAAlgorithmRequest(**json) + ) @blp.route("/drawCircuit", methods=["POST"]) @blp.arguments( CircuitDrawRequestSchema, - example=dict( - circuit="123", circuit_format="openqasm2" - ), + example=dict(circuit="123", circuit_format="openqasm2"), description="QASM 2.0 String.", ) @blp.response(200, CircuitDrawResponseSchema) diff --git a/app/controller/encoding/encoding_controller.py b/app/controller/encoding/encoding_controller.py index 891ecf3..cf35d50 100644 --- a/app/controller/encoding/encoding_controller.py +++ b/app/controller/encoding/encoding_controller.py @@ -29,7 +29,12 @@ @blp.etag @blp.arguments( BasisEncodingRequestSchema, - example=dict(vector=[1.25, 3.14], integral_bits=3, fractional_bits=3, circuit_format="openqasm2"), + example=dict( + vector=[1.25, 3.14], + integral_bits=3, + fractional_bits=3, + circuit_format="openqasm2", + ), ) @blp.response(200, BasisEncodingResponseSchema) def encoding(json: BasisEncodingRequest): @@ -40,7 +45,8 @@ def encoding(json: BasisEncodingRequest): @blp.route("/angle", methods=["POST"]) @blp.etag @blp.arguments( - AngleEncodingRequestSchema, example=dict(vector=[1.25, 3.14], rotation_axis="x", circuit_format="openqasm2") + AngleEncodingRequestSchema, + example=dict(vector=[1.25, 3.14], rotation_axis="x", circuit_format="openqasm2"), ) @blp.response(200, AngleEncodingResponseSchema) def encoding(json: AngleEncodingRequest): @@ -50,21 +56,29 @@ def encoding(json: AngleEncodingRequest): @blp.route("/amplitude", methods=["POST"]) @blp.etag -@blp.arguments(AmplitudeEncodingRequestSchema, example=dict(vector=[1.25, 3.14], circuit_format="openqasm2")) +@blp.arguments( + AmplitudeEncodingRequestSchema, + example=dict(vector=[1.25, 3.14], circuit_format="openqasm2"), +) @blp.response(200, AmplitudeEncodingResponseSchema) def encoding(json: AmplitudeEncodingRequest): if json: - return encoding_service.generate_amplitude_encoding(AmplitudeEncodingRequest(**json)) + return encoding_service.generate_amplitude_encoding( + AmplitudeEncodingRequest(**json) + ) @blp.route("/schmidt", methods=["POST"]) @blp.arguments( - SchmidtDecompositionRequestSchema, example=dict(vector=[1.25, 3.14, 0, 1], circuit_format="openqasm2") + SchmidtDecompositionRequestSchema, + example=dict(vector=[1.25, 3.14, 0, 1], circuit_format="openqasm2"), ) @blp.response(200, SchmidtDecompositionResponseSchema) def encoding(json: SchmidtDecompositionRequest): if json: - return encoding_service.generate_schmidt_decomposition(SchmidtDecompositionRequest(**json)) + return encoding_service.generate_schmidt_decomposition( + SchmidtDecompositionRequest(**json) + ) # TODO diff --git a/app/model/algorithm_request.py b/app/model/algorithm_request.py index be406b7..85f10e2 100644 --- a/app/model/algorithm_request.py +++ b/app/model/algorithm_request.py @@ -17,7 +17,16 @@ class HHLAlgorithmRequestSchema(ma.Schema): class QAOAAlgorithmRequest: - def __init__(self, pauli_op_string, gammas=None, betas=None, initial_state = None, mixer = None, reps = 1, circuit_format="openqasm2"): + def __init__( + self, + pauli_op_string, + gammas=None, + betas=None, + initial_state=None, + mixer=None, + reps=1, + circuit_format="openqasm2", + ): if gammas is None: gammas = [1] if reps == None else [1] * reps self.initial_state = initial_state @@ -53,6 +62,7 @@ class QFTAlgorithmRequestSchema(ma.Schema): barriers = ma.fields.Bool() circuit_format = ma.fields.String() + class QPEAlgorithmRequest: def __init__(self, n_eval_qubits, unitary, circuit_format="openqasm2"): self.n_eval_qubits = n_eval_qubits @@ -67,7 +77,9 @@ class QPEAlgorithmRequestSchema(ma.Schema): class VQEAlgorithmRequest: - def __init__(self, observable, ansatz = None, parameters = None, circuit_format="openqasm2"): + def __init__( + self, observable, ansatz=None, parameters=None, circuit_format="openqasm2" + ): self.ansatz = ansatz self.parameters = parameters self.observable = observable @@ -82,7 +94,15 @@ class VQEAlgorithmRequestSchema(ma.Schema): class GroverAlgorithmRequest: - def __init__(self, oracle, iterations = None, reflection_qubits = None, initial_state = None, barriers = None, circuit_format="openqasm2"): + def __init__( + self, + oracle, + iterations=None, + reflection_qubits=None, + initial_state=None, + barriers=None, + circuit_format="openqasm2", + ): self.oracle = oracle self.iterations = iterations self.reflection_qubits = reflection_qubits @@ -110,7 +130,7 @@ def __init__( parameterized=False, initial_state=None, epsilon=0.25, - circuit_format="openqasm2" + circuit_format="openqasm2", ): self.adj_matrix = adj_matrix self.betas = betas @@ -151,7 +171,9 @@ class TSPQAOAAlgorithmRequestSchema(ma.Schema): class KnapsackQAOAAlgorithmRequest: - def __init__(self, items, max_weights, p, betas, gammas, circuit_format="openqasm2"): + def __init__( + self, items, max_weights, p, betas, gammas, circuit_format="openqasm2" + ): self.items = items self.max_weights = max_weights self.p = p diff --git a/app/model/circuit_response.py b/app/model/circuit_response.py index 172eeb2..ed80c26 100644 --- a/app/model/circuit_response.py +++ b/app/model/circuit_response.py @@ -18,13 +18,16 @@ import qiskit.qasm3 + def export_circuit(circuit, input): - if (hasattr(input, 'parameterized') and input.parameterized) or input.circuit_format == "openqasm3": + if ( + hasattr(input, "parameterized") and input.parameterized + ) or input.circuit_format == "openqasm3": return qiskit.qasm3.dumps(circuit) elif input.circuit_format == "openqasm2": return circuit.qasm() else: - return 'format unsupported' + return "format unsupported" class CircuitResponse: diff --git a/app/model/encoding_request.py b/app/model/encoding_request.py index bde6e93..5cd01e2 100644 --- a/app/model/encoding_request.py +++ b/app/model/encoding_request.py @@ -4,7 +4,9 @@ class BasisEncodingRequest: - def __init__(self, vector, integral_bits, fractional_bits, circuit_format="openqasm2"): + def __init__( + self, vector, integral_bits, fractional_bits, circuit_format="openqasm2" + ): self.vector = vector self.integral_bits = integral_bits self.fractional_bits = fractional_bits diff --git a/app/services/algorithm_service.py b/app/services/algorithm_service.py index 0606cea..7656742 100644 --- a/app/services/algorithm_service.py +++ b/app/services/algorithm_service.py @@ -251,7 +251,12 @@ def generate_max_cut_qaoa_circuit(request: MaxCutQAOAAlgorithmRequest): ) return CircuitResponse( - circuit, "algorithm/qaoa", circuit.num_qubits, circuit.depth(), request, circuit_language="openqasm", + circuit, + "algorithm/qaoa", + circuit.num_qubits, + circuit.depth(), + request, + circuit_language="openqasm", ) @@ -262,7 +267,12 @@ def generate_tsp_qaoa_circuit(input: TSPQAOAAlgorithmRequest): gammas = input.gammas circuit = TSPQAOAAlgorithm.create_circuit(np.array(adj_matrix), p, betas, gammas) return CircuitResponse( - circuit, "algorithm/tspqaoa", circuit.num_qubits, circuit.depth(), input,circuit_language="openqasm", + circuit, + "algorithm/tspqaoa", + circuit.num_qubits, + circuit.depth(), + input, + circuit_language="openqasm", ) diff --git a/app/services/encoding_service.py b/app/services/encoding_service.py index d725c04..7000983 100644 --- a/app/services/encoding_service.py +++ b/app/services/encoding_service.py @@ -28,7 +28,12 @@ def generate_basis_encoding(input: BasisEncodingRequest): ) return CircuitResponse( - circuit, "encoding/basis", circuit.num_qubits, circuit.depth(), input,circuit_language="openqasm" + circuit, + "encoding/basis", + circuit.num_qubits, + circuit.depth(), + input, + circuit_language="openqasm", )