Skip to content

Commit

Permalink
Fix test.
Browse files Browse the repository at this point in the history
Remove optimization level 3 from transpile for aws (not working for IONQ Aria 2)
  • Loading branch information
DominikVoigt committed Nov 11, 2023
1 parent f290030 commit 68cff9e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
5 changes: 2 additions & 3 deletions app/aws_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
from qiskit import QiskitError


def get_qpu(access_key, secret_access_key, qpu_name, region_name='eu-west-2'):
def get_qpu(access_key, secret_access_key, qpu_name):
boto_session = boto3.Session(
aws_access_key_id=access_key,
aws_secret_access_key=secret_access_key,
region_name=region_name
aws_secret_access_key=secret_access_key
)
session = AwsSession(boto_session)
provider = AWSBraketProvider()
Expand Down
2 changes: 1 addition & 1 deletion app/circuit_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def get_multi_qubit_gate_depth(transpiled_circuit):
def get_number_of_measurement_operations(transpiled_circuit):
""" Get number of measurement operations in the transpiled circuit """
transpiled_dag = circuit_to_dag(transpiled_circuit)
return transpiled_dag.count_ops().get('measure')
return transpiled_dag.count_ops().get('measure', 0)
13 changes: 6 additions & 7 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def transpile_circuit():
abort(400)

try:
print("circuit", circuit)
print(f"Circuit:\n{circuit}")
non_transpiled_depth_old = 0
non_transpiled_depth = circuit.depth()
while non_transpiled_depth_old < non_transpiled_depth:
Expand All @@ -115,7 +115,6 @@ def transpile_circuit():
app.logger.warn(f"{short_impl_name} not found.")
abort(404)
except Exception as e:

app.logger.info(f"Transpile {short_impl_name} for {qpu_name}: {str(e)}")
return jsonify({'error': str(e)}), 200

Expand All @@ -141,9 +140,11 @@ def transpile_circuit():
abort(404)

try:
transpiled_circuit = transpile(circuit, backend=backend, optimization_level=3)
print("Transpiled Circuit")
print(transpiled_circuit)
if provider == 'aws':
transpiled_circuit = transpile(circuit, backend=backend)
else:
transpiled_circuit = transpile(circuit, backend=backend, optimization_level=3)
print(f"Transpiled Circuit:\n{transpiled_circuit}")
width = circuit_analysis.get_width_of_circuit(transpiled_circuit)
depth = transpiled_circuit.depth()
total_number_of_operations = transpiled_circuit.size()
Expand All @@ -152,8 +153,6 @@ def transpile_circuit():
number_of_single_qubit_gates = total_number_of_operations - number_of_multi_qubit_gates - \
number_of_measurement_operations
multi_qubit_gate_depth, transpiled_circuit = circuit_analysis.get_multi_qubit_gate_depth(transpiled_circuit)
print("After all")
print(transpiled_circuit)

except TranspilerError:
app.logger.info(f"Transpile {short_impl_name} for {qpu_name}: too many qubits required")
Expand Down
36 changes: 35 additions & 1 deletion test/test_transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_transpile_circuit_perth_file(self):
'impl-language': 'openqasm',
'qpu-name': "ibm_perth",
'input-params': {},
'token': os.environ["QISKIT_TOKEN"]
'token': os.environ.get("QISKIT_TOKEN", "")
}

# send the request
Expand Down Expand Up @@ -193,6 +193,40 @@ def test_transpile_shor_perth_url_qasm(self):
self.assertEqual(r.status_code, 202)
print(r.headers.get("Location"))

def test_transpile_file_qasm_aws(self):
circuit = random_circuit(5, 3, seed=42)
circuit = circuit.qasm()
impl_data = base64.b64encode(circuit.encode()).decode()
# prepare the request
request = {
'impl-data': impl_data,
'impl-language': 'OpenQASM',
'provider': 'aws',
'qpu-name': "Aria 2",
'input-params': {},
'aws_access_key_id': os.environ.get("QISKIT_AWS_TOKEN", ""),
'aws_secret_access_key': os.environ.get("QISKIT_AWS_SECRET", "")
}

# send the request
response = self.client.post('/qiskit-service/api/v1.0/transpile',
json=request)

self.assertEqual(response.status_code, 200)
json_data = response.get_json()
self.assertIn("width", json_data)
self.assertIn("depth", json_data)
self.assertIsNotNone(json_data['depth'])
self.assertIsNotNone(json_data['width'])
self.assertIn('transpiled-qasm', json_data)
self.assertIn("total-number-of-operations", json_data)
self.assertIn("number-of-multi-qubit-gates", json_data)
self.assertIn("multi-qubit-gate-depth", json_data)
self.assertIsNotNone(json_data["total-number-of-operations"])
self.assertIsNotNone(json_data["number-of-multi-qubit-gates"])
self.assertIsNotNone(json_data["multi-qubit-gate-depth"])
self.assertIsNotNone(json_data.get('transpiled-qasm'))

def test_transpile_circuit_nairobi_file_qasm(self):

# prepare the request
Expand Down

0 comments on commit 68cff9e

Please sign in to comment.