Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mbeisel committed Jan 11, 2024
1 parent 9ab4dc9 commit 936a1ee
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 36 deletions.
37 changes: 18 additions & 19 deletions app/controller/algorithms/algorithm_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"])
Expand All @@ -164,7 +163,7 @@ def encoding(json):
gammas=[1.0],
p=1,
parameterized=False,
circuit_format="openqasm2"
circuit_format="openqasm2",
),
)
@blp.response(200, CircuitResponseSchema)
Expand All @@ -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)
Expand Down
26 changes: 20 additions & 6 deletions app/controller/encoding/encoding_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand Down
32 changes: 27 additions & 5 deletions app/model/algorithm_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions app/model/circuit_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion app/model/encoding_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions app/services/algorithm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


Expand All @@ -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",
)


Expand Down
7 changes: 6 additions & 1 deletion app/services/encoding_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


Expand Down

0 comments on commit 936a1ee

Please sign in to comment.