Creating a simple quantum circuit using qandle

Import the required libraries

import qandle
import torch

Define a Quantum Circuit

circuit = qandle.Circuit(
    layers=[
        # embedding layer
        qandle.AngleEmbedding(qubits=[0, 1], name="input0"),
        # trainable layer, with random initialization
        qandle.RX(qubit=1),
        # trainable layer, with fixed initialization
        qandle.RY(qubit=0, theta=torch.tensor(0.2)),
        # data reuploading
        qandle.RX(qubit=0, name="input1"),
        # disable quantum weight remapping
        qandle.RY(qubit=1, remapping=None),
        qandle.CNOT(control=0, target=1),
        qandle.MeasureProbability(),
    ]
)

# print the circuit as a torch module
circuit
Circuit(
  (circuit): UnsplittedCircuit(
    (layers): ModuleList(
      (0): AngleEmbedding_rx
      (1): RX_1 (0.50)
      (2): RY_0 (0.62)
      (3): RX_0 (input1)
      (4): RY_1 (0.77)
      (5): CNOT 0|1
      (6): M
    )
  )
)

Run the circuit

input_state = torch.rand(circuit.num_qubits, dtype=torch.float) # random input state
input0 = torch.tensor([0.2, 0.1])
input1 = torch.tensor(0.5) 

circuit(input_state, input0=input0, input1=input1)
tensor([0.8024, 0.6781], grad_fn=<TransposeBackward0>)

Export it to QASM

qasm3 = qandle.convert_to_qasm(circuit, qasm_version=3)
print(qasm3)
OPENQASM 3.0;
qubit[2] q;
bit[2] c;
input float angle_2126134613129426432_0;
input float angle_2126134613129426432_1;
input float input1;
output float measured_802107360653707776_0;
output float measured_802107360653707776_1;
rx(angle_2126134613129426432_0) q[0];
rx(angle_2126134613129426432_1) q[1];
rx(0.5042577385902405) q[1];
ry(0.6200729012489319) q[0];
rx(input1) q[0];
ry(0.7730306386947632) q[1];
cx q[0], q[1];
measure q[0] -> c[0];
measure q[1] -> c[1];

Visualize the circuit

Use circuit.draw() to get a simple string representation of the circuit.

print(circuit.draw())
q0─AngleEmbedding_rx─────────────RY_0 (0.62)─RX_0 (input1)─────────────⬤─M─
q1─AngleEmbedding_rx─RX_1 (0.50)───────────────────────────RY_1 (0.77)─⊕─M─