Executing your first program
Before jumping into the deeper workings of QAT, let’s run a simple program against an echo engine. This doesn’t do much useful execution, but compiling and executing against it is very simple, and fortunately, compiling and executing against actual hardware or useful simulators isn’t much more complicated.
We start by defining in a hardware loader, which is used to load in a hardware model by different means. This particular loader just creates a mock-up of hardware with a ring topology.
from qat.model.loaders.lucy import LucyModelLoader
loader = LucyModelLoader(qubit_count=8)
We use the model to constuct an echo pipeline that compiles and executes using an “echo mode”
from qat.pipelines.waveform import EchoPipeline, PipelineConfig
config = PipelineConfig(name="echo_pipeline")
pipeline = EchoPipeline(loader=loader, config=config)
We will run a QASM2 program that creates and measures a bell state.
from compiler_config.config import CompilerConfig, QuantumResultsFormat
qasm_str = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
h q[0];
cx q[0], q[1];
measure q -> c;
"""
config = CompilerConfig(results_format=QuantumResultsFormat().binary_count())
Now we can compile and execute the program against the pipeline
from qat import QAT
core = QAT()
results, metrics = core.run(qasm_str, compiler_config=config, pipeline=pipeline)
print(results)
{'c': {'11': 1000}}
Setting up using QAT config
QAT also provides a way to conveniently set up compilation and execution pipelines as a configurational YAML file. If we write a file called qatconfig.yaml with the following contents:
1HARDWARE:
2- name: lucy
3 type: qat.model.loaders.lucy.LucyModelLoader
4 config:
5 qubit_count: 8
6
7PIPELINES:
8- name: echo_pipeline
9 pipeline: qat.pipelines.waveform.EchoPipeline
10 hardware_loader: lucy
We can then load it in to QAT and execute as before:
1from qat import QAT
2from compiler_config.config import CompilerConfig, QuantumResultsFormat
3
4# Set up compile and execute pipelines
5core = QAT(qatconfig="qatconfig.yaml")
6
7# Set up the input program
8qasm_str = """
9OPENQASM 2.0;
10include "qelib1.inc";
11qreg q[2];
12creg c[2];
13h q[0];
14cx q[0], q[1];
15measure q -> c;
16"""
17config = CompilerConfig(repeats=1000, results_format=QuantumResultsFormat().binary_count())
18
19# Execute using QAT
20results, metrics = core.run(qasm_str, compiler_config=config, pipeline="echo_pipeline")