2

As an example, consider the following Circuit defined by using the Python amazon-braket-sdk:

from braket.circuits import Circuit

bell = Circuit() bell.h(0) bell.cnot(0, 1)

How can I get the corresponding QASM code? If possible, I would like to do it directly, namely without going through any other intermediate library.

SimoneGasperini
  • 1,644
  • 1
  • 3
  • 18

2 Answers2

6

You can get OpenQASM output from Braket circuits by using the following code:

from braket.circuits import Circuit
from braket.circuits.serialization import IRType

bell = Circuit().h(0).cnot(0, 1) qasm_ir = bell.to_ir(IRType.OPENQASM) print(qasm_ir.source)

You can read more about to_ir here.

I hope that helps!

Milan-aws
  • 76
  • 2
4

Amazon Braket does not have native support for converting circuits to OpenQASM 2. So besides implementing your own Python script, there's not currently a way to do this that does not involve going through an "intermediate" library.

As mentioned by @Callum in the comments, you could go through pytket + pytket-braket:

from pytket.qasm import circuit_to_qasm_str
from pytket.extensions.braket import braket_to_tk

circuit = # Amazon Braket circuit qasm_str = circuit_to_qasm_str(braket_to_tk(circuit))

You could also go through qBraid:

from qbraid import circuit_wrapper

circuit = # Amazon Braket circuit qasm_str = circuit_wrapper(circuit).transpile("qasm2")

Integrations through mitiq, amazon-braket-pennylane-plugin-python, or qiskit-braket-provider could also be equally if not more efficient than direct qasm conversions depending on your use-case.

Edit: Was not considering OpenQASM 3 conversions, which are supported! See answer above :)

ryanhill1
  • 2,668
  • 1
  • 11
  • 39