2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Not sure how to do this directly in braket but using the braket_to_tkfunction from the pytket-braket extension will give you a pytket Circuit. You can then convert the Circuit to qasm with the circuit_to_qasm function. $\endgroup$
    – Callum
    May 31 at 16:20

2 Answers 2

6
$\begingroup$

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!

$\endgroup$
4
$\begingroup$

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 :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.