Example usage
Maestro offers a Python interface (MaestroPython) that allows you to integrate intelligent quantum simulation into your development workflows with minimal setup. Below is a simple example that demonstrates how to simulate a QAOA-style circuit written in OpenQASM using Maestro’s simulator selection engine.
from maestro import MaestroPython as maestro
from maestro import qiskit_interop as inter
# Uses Qiskit to translate from QASM.
from qiskit import QuantumCircuit
def simulate_circuit(qasm_circuit, shots):
circuit = QuantumCircuit.from_qasm_str(qasm_circuit)
composer_circuit = inter.FromQiskit(qiskit_circuit)
num_qubits = circuit.num_qubits
network = Composer.NetworkFactory.CreateWithLists(
Composer.NetworkType.SimpleNetwork, [num_qubits], [num_qubits]
)
# Creates a simulator with all possible simulators.
network.CreateSimulator()
results = network.RepeatedExecuteOnHost(composer_circuit, 0, shots)
return results
circuit = "OPENQASM 2.0;" + \
"include \"qelib1.inc\";" + \
"qreg q[3];" + \
"creg meas[3];" + \
"h q[0];" + \
"h q[1];" + \
"rzz(5.094) q[0],q[1];" + \
"rzz(5.094) q[0],q[2];" + \
"measure q[0] -> meas[0];" + \
"measure q[1] -> meas[1];" + \
"measure q[2] -> meas[2];"
simulate_circuit(circuit, shots=10000)
What’s Happening Here?
Function | Purpose |
---|---|
QuantumCircuit.from_qasm_str(...) |
Parses an OpenQASM string into a Qiskit QuantumCircuit object. |
inter.FromQiskit(circuit) |
Converts the Qiskit circuit into Maestro’s internal circuit format. |
circuit.num_qubits |
Gets the number of qubits to configure the simulation network. |
NetworkFactory.CreateWithLists(...) |
Initializes a simple simulation network with the specified qubit layout. |
network.CreateSimulator() |
Automatically selects and initializes the most efficient simulator backend. |
network.RepeatedExecuteOnHost(...) |
Runs the circuit for the specified number of shots, leveraging Maestro’s optimization features like multi-shot caching and backend selection. |
Customization Tips
- Simulator Selection: By default, Maestro runs internal benchmarks or model-based estimations to select the backend. You can override this behavior by explicitly setting the simulator via Maestro’s interface.
- Distributed Topologies: Replace SimpleNetwork with more complex network types (e.g., for multi-device or entangled-distributed circuits).