def test_id(self): """Tests that the id attribute can be set.""" template = qml.ParticleConservingU1(weights=np.array([[[0.2, -0.6]]]), wires=range(2), init_state=np.array([1, 1]), id="a") assert template.id == "a"
def test_operations(self): """Test the correctness of the ParticleConservingU1 template including the gate count and order, the wires each operation acts on and the correct use of parameters in the circuit.""" qubits = 4 layers = 2 weights = np.random.random(size=(layers, qubits - 1, 2)) gates_per_u1 = 16 gates_per_layer = gates_per_u1 * (qubits - 1) gate_count = layers * gates_per_layer + 1 gates_u1 = [ qml.CZ, qml.CRot, qml.PhaseShift, qml.CNOT, qml.PhaseShift, qml.CNOT, qml.PhaseShift, ] gates_ent = gates_u1 + [qml.CZ, qml.CRot] + gates_u1 wires = list(range(qubits)) nm_wires = [wires[l:l + 2] for l in range(0, qubits - 1, 2)] nm_wires += [wires[l:l + 2] for l in range(1, qubits - 1, 2)] op = qml.ParticleConservingU1(weights, wires, init_state=np.array([1, 1, 0, 0])) queue = op.expand().operations assert gate_count == len(queue) # check initialization of the qubit register assert isinstance(queue[0], qml.BasisEmbedding) # check all quantum operations idx_CRot = 8 for l in range(layers): for i in range(qubits - 1): exp_wires = self._wires_gates_u1(nm_wires[i]) phi = weights[l, i, 0] theta = weights[l, i, 1] for j, exp_gate in enumerate(gates_ent): idx = gates_per_layer * l + gates_per_u1 * i + j + 1 # check that the gates are applied in the right order assert isinstance(queue[idx], exp_gate) # check the wires the gates act on assert queue[idx].wires.tolist() == exp_wires[j] # check that parametrized gates take the parameters \phi and \theta properly if exp_gate is qml.CRot: if j < idx_CRot: exp_params = [-phi, np.pi, phi] if j > idx_CRot: exp_params = [phi, np.pi, -phi] if j == idx_CRot: exp_params = [0, 2 * theta, 0] assert queue[idx].parameters == exp_params elif exp_gate is qml.PhaseShift: if j < idx_CRot: exp_params = -phi if j == idx_CRot / 2: exp_params = phi if j > idx_CRot: exp_params = phi if j == (3 * idx_CRot + 2) / 2: exp_params = -phi assert queue[idx].parameters == exp_params
def circuit_template(weights): qml.ParticleConservingU1(weights, range(2), init_state=np.array([1, 1])) return qml.expval(qml.PauliZ(0))
def circuit(): qml.ParticleConservingU1(weights=weights, wires=wires, init_state=init_state) return qml.expval(qml.PauliZ(0))
def circuit2(): qml.ParticleConservingU1(weights, wires=["z", "a", "k"], init_state=init_state) return qml.expval(qml.Identity("z"))
def circuit(): qml.ParticleConservingU1(weights, wires=range(3), init_state=init_state) return qml.expval(qml.Identity(0))