Package: qbraid
The qBraid-SDK is a Python toolkit for cross-framework abstraction,
transpilation, and execution of quantum programs.FeaturesUnified quantum frontend interface.Transpilequantum circuits between
supported packages. Leverage the capabilities of multiple frontends throughsimple, consistent protocols.Build once, target many.Createquantum programs using your preferred
circuit-building package, andexecuteon any backend that interfaces with
a supported frontend.Benchmark, compare, interpret results. Built-incompatiblepost-processing
enables comparing results between runs andacross backends.Installation & SetupFor the best experience, install the qBraid-SDK environment onlab.qbraid.com. Login (orcreate an account) and follow the steps toinstall an environment.Using the SDK on qBraid Lab means direct, pre-configured access to allAmazon Braket supported deviceswith no additional access keys or API tokens required. SeeqBraid Quantum Jobsfor more.Local installThe qBraid-SDK, and all of its dependencies, can also be installed using pip:pipinstallqbraidYou can alsoinstall from sourceby cloning this repository and running a pip install command in the root directory of the repository:gitclonehttps://github.com/qBraid/qBraid.gitcdqBraid
pipinstall-e'.[all]'Note: The qBraid-SDK requires Python 3.9 or greater.If using locally, follow linked instructions to configure yourqBraid,AWS,
andIBMQcredentials.Check versionYou can view the version of the qBraid-SDK you have installed within Python using the following:In[1]:importqbraidIn[2]:qbraid.__version__Documentation & TutorialsqBraid documentation is available atdocs.qbraid.com.See also:API ReferenceUser GuideExample NotebooksQuickstartTranspilerConstruct a quantum program of any supported program type.Below,SUPPORTED_QPROGRAMSmaps shorthand identifiers for supported quantum programs, each corresponding to a type in the typedQPROGRAMUnion.
For example, 'qiskit' maps toqiskit.QuantumCircuitinQPROGRAM. Notably, 'qasm2' and 'qasm3' both represent raw OpenQASM strings.
This arrangement simplifies targeting and transpiling between different quantum programming frameworks.>>>fromqbraidimportSUPPORTED_QPROGRAMS>>>SUPPORTED_QPROGRAMS{'cirq':'cirq.circuits.circuit.Circuit','qiskit':'qiskit.circuit.quantumcircuit.QuantumCircuit','pyquil':'pyquil.quil.Program','pytket':'pytket._tket.circuit.Circuit','braket':'braket.circuits.circuit.Circuit','openqasm3':'openqasm3.ast.Program','qasm2':'str','qasm3':'str'}Pass any quantum program of typeqbraid.QPROGRAMto thecircuit_wrapper()and specify a target package
fromSUPPORTED_QPROGRAMSto "transpile" your circuit to a new program type:>>>fromqbraidimportcircuit_wrapper>>>fromqbraid.interfaceimportrandom_circuit>>>qiskit_circuit=random_circuit("qiskit")>>>cirq_circuit=circuit_wrapper(qiskit_circuit).transpile("cirq")>>>print(qiskit_circuit)┌────────────┐q_0:──■──┤Rx(3.0353)├┌─┴─┐└───┬────┬───┘q_1:┤H├────┤√X├────└───┘└────┘>>>print(cirq_circuit)0:───@───Rx(0.966π)───│1:───H───X^0.5────────The same functionality can be achieved using the underlyingconvert_to_package()function directly:>>>fromqbraidimportconvert_to_package>>>cirq_circuit=convert_to_package(qiskit_circuit,"cirq")Behind the scenes, the qBraid-SDK usesnetworkxto create a directional graph that maps all possible conversions between supported program types:fromqbraid.transpilerimportConversionGraphfromqbraid.visualizationimportplot_conversion_graphgraph=ConversionGraph()plot_conversion_graph(graph)You can use the native conversions supported by qBraid, or define your own custom nodes and/or edges. Seeexample.Devices & JobsSearch for quantum backend(s) on which to execute your program.>>>fromqbraidimportget_devices>>>get_devices()Devicestatusupdated0minutesagoDeviceIDStatus---------------aws_oqc_lucyONLINEaws_ionq_aria2OFFLINEaws_rigetti_aspen_m3ONLINEibm_q_brisbaneONLINE...Apply thedevice_wrapper(), and send quantum jobs to any supported backend,
from any supported program type:>>>fromqbraidimportdevice_wrapper,get_jobs>>>aws_device=device_wrapper("aws_oqc_lucy")>>>ibm_device=device_wrapper("ibm_q_brisbane")>>>aws_job=aws_device.run(qiskit_circuit,shots=1000)>>>ibm_job=ibm_device.run(cirq_circuit,shots=1000)>>>get_jobs()Displaying2mostrecentjobs:JobIDSubmittedStatus---------------------aws_oqc_lucy-exampleuser-qjob-zzzzzzz...2023-05-21T21:13:47.220ZQUEUEDibm_q_brisbane-exampleuser-qjob-xxxxxxx...2023-05-21T21:13:48.220ZRUNNING...Compare results in a consistent, unified format:>>>aws_result=aws_job.result()>>>ibm_result=ibm_job.result()>>>aws_result.measurement_counts(){'00':483,'01':14,'10':486,'11':17}>>>ibm_result.measurement_counts(){'00':496,'01':12,'10':479,'11':13}Local account setupTo use the qBraid-SDK locally (outside of qBraid Lab), you must add your account
credentials:Create a qBraid account or log in to your existing account by visitingaccount.qbraid.comCopy your API Key token from the left side of
youraccount page:Save your API key from step 2 by callingQbraidSession.save_config():fromqbraid.apiimportQbraidSessionsession=QbraidSession(api_key='API_KEY')session.save_config()The command above stores your credentials locally in a configuration file~/.qbraid/qbraidrc,
where~corresponds to your home ($HOME) directory. Once saved, you can then connect to the
qBraid API and leverage functions such asget_devices()andget_jobs().Load Account from Environment VariablesAlternatively, the qBraid-SDK can discover credentials from environment
variables:exportJUPYTERHUB_USER='USER_EMAIL'exportQBRAID_API_KEY='QBRAID_API_KEY'Then instantiate the session without any argumentsfromqbraid.apiimportQbraidSessionsession=QbraidSession()Launch on qBraidThe "Launch on qBraid" button (below) can be added to any public GitHub
repository. Clicking on it automaically opens qBraid Lab, and performs agit cloneof the project repo into your account's home directory. Copy the
code below, and replaceYOUR-USERNAMEandYOUR-REPOSITORYwith your GitHub
info.Use the badge in your project'sREADME.md:[<img src="https://qbraid-static.s3.amazonaws.com/logos/Launch_on_qBraid_white.png" width="150">](https://account.qbraid.com?gitHubUrl=https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git)Use the badge in your project'sREADME.rst:..image::https://qbraid-static.s3.amazonaws.com/logos/Launch_on_qBraid_white.png:target:https://account.qbraid.com?gitHubUrl=https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git:width:150pxContributingInterested in contributing code, or making a PR? SeeCONTRIBUTING.mdFor feature requests and bug reports:Submit an issueFor discussions, and specific questions about the qBraid SDK, qBraid Lab, or
other topics,join our discord communityFor questions that are more suited for a forum, post toQuantum Computing Stack Exchangewith theqbraidtag.Want your open-source project featured as its own runtime environment on
qBraid Lab? Fill out ourNew Environment Request FormLicenseGNU General Public License v3.0