def circuit(params, wires, size=None, layers=None): """Define the quantum circuit as a stack of VQC layers Args: params (Array): Variational parameters in the form of NumPy array wires (List[int]): List of qubit indices size (int): Number of qubits layers (List[int]): List of VQCs stacked up """ param_size = [models.param_shape(i, size) for i in layers] param_idx_end = list(itertools.accumulate(param_size)) param_idx_start = [0] + param_idx_end[:-1] for idx_start, idx_end, layer in zip(param_idx_start, param_idx_end, layers): models.__dict__[f'circuit{layer:0>2d}'](params[idx_start:idx_end], wires)
def build_dummy_model(layers, size=2): # Define the quantum device device, wires = qml.device('default.qubit', wires=size), list(range(size)) param_size = [models.param_shape(i, size) for i in layers] # Define the variational parameters params = 2 * np.pi * (np.random.rand(sum(param_size)) - 0.5) # Define the Hamiltonian operators operators = [qml.PauliZ(i) @ qml.PauliZ(i + 1) for i in range(size - 1)] + \ [qml.PauliX(i) for i in range(size)] coeffs = np.array([0.5 for i in range(size - 1)] + [1 for i in range(size)]) # Define the QNodeCollection qnodes = qml.map(circuit, operators, device, measure="expval") # Evaluate the QNodeCollection def HNode(_params): return np.dot(coeffs, qnodes(_params, size=size, layers=layers)) Op, OpG = HNode, qml.grad(HNode) return Op, OpG, params, param_size
params (Array): Variational parameters in the form of NumPy array wires (List[int]): List of qubit indices size (int): Number of qubits layers (List[int]): List of VQCs stacked up """ param_size = [models.param_shape(i, size) for i in layers] param_idx_end = list(itertools.accumulate(param_size)) param_idx_start = [0] + param_idx_end[:-1] for idx_start, idx_end, layer in zip(param_idx_start, param_idx_end, layers): models.__dict__[f'circuit{layer:0>2d}'](params[idx_start:idx_end], wires) # Define the quantum device dev, wires = qml.device('default.qubit', wires=SIZE), list(range(SIZE)) param_sizes = [models.param_shape(i, SIZE) for i in LAYERS] # Define the variational parameters params = 2 * np.pi * (np.random.rand(sum(param_sizes)) - 0.5) # Define the Hamiltonian operators operators, coeffs = Hamiltonian() # Define the QNodeCollection qnodes = qml.map(circuit, operators, dev, measure="expval") def HNode(params): # Evaluate the QNodeCollection return np.dot(coeffs, qnodes(params, size=SIZE, layers=LAYERS))