def classifier_circuit(in_data, x): qml.RX(in_data, wires=[0]) qml.CNOT(wires=[0, 1]) qml.RY(-1.6, wires=[0]) qml.RY(in_data, wires=[1]) qml.CNOT(wires=[1, 0]) qml.RX(x, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval.PauliZ(0)
def circuit(x, y, z): qml.RX(x, [0]) qml.CNOT([0, 1]) qml.RY(-1.6, [0]) qml.RY(y, [1]) qml.CNOT([1, 0]) qml.RX(z, [0]) qml.CNOT([0, 1]) return qml.expval.PauliZ(0)
def circuit(x, y, z): qml.RX(x, wires=[0]) qml.CNOT(wires=[0, 1]) qml.RY(-1.6, wires=[0]) qml.RY(y, wires=[1]) qml.CNOT(wires=[1, 0]) qml.RX(z, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval.PauliZ(0)
def test_simple_circuits(self): default_qubit = qml.device('default.qubit', wires=4) for dev in self.devices: gates = [ qml.PauliX(wires=0), qml.PauliY(wires=1), qml.PauliZ(wires=2), qml.S(wires=3), qml.T(wires=0), qml.RX(2.3, wires=1), qml.RY(1.3, wires=2), qml.RZ(3.3, wires=3), qml.Hadamard(wires=0), qml.Rot(0.1, 0.2, 0.3, wires=1), qml.CRot(0.1, 0.2, 0.3, wires=[2, 3]), qml.Toffoli(wires=[0, 1, 2]), qml.SWAP(wires=[1, 2]), qml.CSWAP(wires=[1, 2, 3]), qml.U1(1.0, wires=0), qml.U2(1.0, 2.0, wires=2), qml.U3(1.0, 2.0, 3.0, wires=3), qml.CRX(0.1, wires=[1, 2]), qml.CRY(0.2, wires=[2, 3]), qml.CRZ(0.3, wires=[3, 1]), qml.CZ(wires=[2, 3]), qml.QubitUnitary(np.array([[1, 0], [0, 1]]), wires=2), ] layers = 3 np.random.seed(1967) gates_per_layers = [ np.random.permutation(gates).numpy() for _ in range(layers) ] for obs in { qml.PauliX(wires=0), qml.PauliY(wires=0), qml.PauliZ(wires=0), qml.Identity(wires=0), qml.Hadamard(wires=0) }: if obs.name in dev.observables: def circuit(): """4-qubit circuit with layers of randomly selected gates and random connections for multi-qubit gates.""" qml.BasisState(np.array([1, 0, 0, 0]), wires=[0, 1, 2, 3]) for gates in gates_per_layers: for gate in gates: if gate.name in dev.operations: qml.apply(gate) return qml.expval(obs) qnode_default = qml.QNode(circuit, default_qubit) qnode = qml.QNode(circuit, dev) assert np.allclose(qnode(), qnode_default(), atol=1e-3)
def circuit(x, y, z): qml.RX(x, [0]) qml.RZ(y, [0]) qml.CNOT([0, 1]) qml.RY(y, [0]) qml.RX(z, [0]) return qml.expval.PauliY(0), qml.expval.PauliZ(1)
def circuit(x, y, z): qml.RX(x, wires=[0]) qml.RZ(y, wires=[0]) qml.CNOT(wires=[0, 1]) qml.RY(y, wires=[0]) qml.RX(z, wires=[0]) return qml.expval(qml.PauliY(0)), qml.expval(qml.PauliZ(1))
def circuit(a, b, c): qml.RX(a, wires=0) qml.RY(b, wires=1) qml.CNOT(wires=[1, 2]) qml.RX(c, wires=2) qml.CNOT(wires=[0, 1]) qml.RZ(c, wires=2) return qml.var(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.var(qml.PauliZ(2))
def test_check_validity(self): """test that the check_validity method correctly determines what operations/observables are supported.""" self.logTestName() dev = qml.device('default.qubit', wires=2) # overwrite the device supported operations and observables dev._operation_map = { 'RX': 0, 'PauliX': 0, 'PauliY': 0, 'PauliZ': 0, 'Hadamard': 0 } dev._observable_map = {'PauliZ': 0, 'Identity': 0} # test a valid queue queue = [ qml.RX(1., wires=0, do_queue=False), qml.PauliY(wires=1, do_queue=False), qml.PauliZ(wires=2, do_queue=False), ] observables = [qml.expval(qml.PauliZ(0, do_queue=False))] dev.check_validity(queue, observables) # test an invalid operation queue = [qml.RY(1., wires=0, do_queue=False)] with self.assertRaisesRegex(qml.DeviceError, "Gate RY not supported"): dev.check_validity(queue, observables) # test an invalid observable with the same name # as a valid operation queue = [qml.PauliY(wires=0, do_queue=False)] observables = [qml.expval(qml.PauliY(0, do_queue=False))] with self.assertRaisesRegex(qml.DeviceError, "Observable PauliY not supported"): dev.check_validity(queue, observables)
def qf(x, y, z): qml.RX(0.4, [0]) qml.Rot(x, y, z, [0]) qml.RY(-0.2, [0]) return qml.expval.PauliZ(0)
def quant_fun(variables): qml.RX(variables[0][1], wires=[0]) qml.RY(variables[1][2], wires=[0]) qml.RY(variables[2], wires=[0]) return qml.expval.PauliZ(0)
def circuit_nn(dummy1, array, dummy2): qml.RY(0.5 * array[0, 1], 0) qml.RY(-0.5 * array[1, 1], 0) qml.RY(array[1, 0], 1) return qml.expval.PauliX(0), qml.expval.PauliX( 1) # returns a 2-vector
def circuit_n1v(dummy1, array, dummy2): qml.RY(0.5 * array[0, 1], 0) qml.RY(-0.5 * array[1, 1], 0) return qml.expval.PauliX(0), # note the comma, returns a 1-vector
def qf(x, y, z): qml.RX(0.4, wires=[0]) qml.Rot(x, y, z, wires=[0]) qml.RY(-0.2, wires=[0]) return qml.expval(qml.PauliZ(0))
def circuit(x): qml.RY(x, wires=[0]) return qml.expval.PauliZ(0)
def circuit(x, target_observable=None): qml.RX(x[0], wires=0) qml.RY(x[1], wires=0) qml.RZ(x[2], wires=0) qml.CNOT(wires=[0, 1]) return qml.expval(qml.Hermitian(target_observable, wires=[0, 1]))
def quant_fun_mdarr(var): qml.RX(var[0, 1], wires=[0]) qml.RY(var[1, 0], wires=[0]) qml.RY(var[1, 1], wires=[0]) return qml.expval.PauliZ(0)
def circuit(x, y, input_state=np.array([0, 0])): qml.BasisState(input_state, wires=[0, 1]) qml.RX(x, wires=[0]) qml.RY(y, wires=[0]) return qml.expval(qml.PauliZ(0))
def circuit(x, q=default_q): qml.RY(x, wires=0) return qml.expval(qml.PauliZ(q))
def quant_fun_mdlist(var): qml.RX(var[0][1], wires=[0]) qml.RY(var[1][0], wires=[0]) qml.RY(var[1][1], wires=[0]) return qml.expval.PauliZ(0)
def circuit(x, y, z): qml.RZ(z, wires=[0]) qml.RY(y, wires=[0]) qml.RX(x, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval.PauliZ(wires=1)
def qf(x): qml.RX(x, wires=[0]) qml.CNOT(wires=[0, 1]) qml.RY(0.4, wires=[0]) qml.RZ(-0.2, wires=[1]) return qml.expval(qml.PauliX(0)), qml.expval(qml.PauliZ(1))
def qf(x, y): qml.RX(x, [0]) qml.RY(x, [0]) return qml.expval.Hermitian(np.diag([y, 1]), 0)
def qf(x): qml.RX(x, [0]) qml.CNOT([0, 1]) qml.RY(0.4, [0]) qml.RZ(-0.2, [1]) return qml.expval.PauliX(0), qml.expval.PauliZ(1)
def quant_fun_flat(var): qml.RX(var[0], wires=[0]) qml.RY(var[1], wires=[0]) qml.RY(var[2], wires=[0]) qml.RX(var[3], wires=[0]) return qml.expval.PauliZ(0)
def qf(x): qml.RX(x, [0]) ev = qml.expval.PauliZ(1) qml.RY(0.5, [0]) return ev
def circuit_n1s(dummy1, array, dummy2): qml.RY(0.5 * array[0, 1], 0) qml.RY(-0.5 * array[1, 1], 0) return qml.expval.PauliX(0) # returns a scalar
def circuit_tfe(phi, theta): qml.RX(phi[0], wires=0) qml.RY(phi[1], wires=1) qml.CNOT(wires=[0, 1]) qml.PhaseShift(theta[0], wires=0) return qml.expval(qml.PauliZ(0))
def circuit(a): qml.RX(a, wires=0) qml.RY(a, wires=0) return qml.var(qml.PauliZ(0))
def circuit(reused_param, other_param): qml.RX(extra_param, wires=[0]) qml.RY(reused_param, wires=[0]) qml.RZ(other_param, wires=[0]) qml.RX(reused_param, wires=[0]) return qml.expval.PauliZ(0)
def qf(x): qml.RX(x, wires=[0]) ev = qml.expval(qml.PauliZ(1)) qml.RY(0.5, wires=[0]) return ev