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 test_obs_queue(self): """Check that peaking at the obs queue works correctly""" self.logTestName() # queue some gates queue = [] queue.append(qml.RX(0.543, wires=[0], do_queue=False)) queue.append(qml.CNOT(wires=[0, 1], do_queue=False)) dev = qml.device('default.qubit', wires=2) # outside of an execution context, error will be raised with self.assertRaisesRegex( ValueError, "Cannot access the observable value queue outside of the execution context!" ): dev.obs_queue # inside of the execute method, it works with self.assertLogs(level='INFO') as l: dev.execute(queue, [qml.expval(qml.PauliX(0, do_queue=False))]) self.assertEqual(len(l.output), 1) self.assertEqual(len(l.records), 1) self.assertIn('INFO:root:[<pennylane.ops.qubit.PauliX object', l.output[0])
def test_projectq_ops(self): results = [-1.0, -1.0] for i, dev in enumerate(self.devices[1:3]): gates = [ qml.PauliX(wires=0), qml.PauliY(wires=1), qml.PauliZ(wires=2), SqrtX(wires=0), SqrtSwap(wires=[3, 0]), ] layers = 3 np.random.seed(1967) gates_per_layers = [ np.random.permutation(gates).numpy() for _ in range(layers) ] def circuit(): """4-qubit circuit with layers of randomly selected gates.""" for gates in gates_per_layers: for gate in gates: if gate.name in dev.operations: qml.apply(gate) return qml.expval(qml.PauliZ(0)) qnode = qml.QNode(circuit, dev) assert np.allclose(qnode(), results[i], atol=1e-3)
def test_execute(self): """check that execution works on supported operations/observables""" self.logTestName() for dev in self.dev.values(): ops = dev.operations exps = dev.observables queue = [] for o in ops: log.debug('Queueing gate %s...', o) op = qml.ops.__getattribute__(o) if op.par_domain == 'A': # skip operations with array parameters, as there are too # many constraints to consider. These should be tested # directly within the plugin tests. continue elif op.par_domain == 'N': params = np.asarray(np.random.random([op.num_params]), dtype=np.int64) else: params = np.random.random([op.num_params]) queue.append( op(*params, wires=list(range(op.num_wires)), do_queue=False)) temp = [isinstance(op, qml.operation.CV) for op in queue] if all(temp): expval = dev.execute(queue, [qml.expval(qml.X(0, do_queue=False))]) else: expval = dev.execute( queue, [qml.expval(qml.PauliX(0, do_queue=False))]) self.assertTrue(isinstance(expval, np.ndarray))
def circuit(): qml.PauliX(wires=[0]) qml.BasisState(np.array([0, 1, 0, 1]), wires=list(range(self.num_subsystems))) return qml.expval.PauliZ(0)
def test_validity(self): """check that execution throws error on unsupported operations/observables""" self.logTestName() for dev in self.dev.values(): ops = dev.operations all_ops = set(qml.ops.__all_ops__) for o in all_ops - ops: op = getattr(qml.ops, o) if op.par_domain == 'A': # skip operations with array parameters, as there are too # many constraints to consider. These should be tested # directly within the plugin tests. continue elif op.par_domain == 'N': params = np.asarray(np.random.random([op.num_params]), dtype=np.int64) else: params = np.random.random([op.num_params]) queue = [ op(*params, wires=list(range(op.num_wires)), do_queue=False) ] temp = isinstance(queue[0], qml.operation.CV) with self.assertRaisesRegex(qml.DeviceError, 'not supported on device'): if temp: expval = dev.execute( queue, [qml.expval(qml.X(0, do_queue=False))]) else: expval = dev.execute( queue, [qml.expval(qml.PauliX(0, do_queue=False))]) exps = dev.observables all_exps = set(qml.ops.__all_obs__) for g in all_exps - exps: op = getattr(qml.ops, g) if op.par_domain == 'A': # skip observables with array parameters, as there are too # many constraints to consider. These should be tested # directly within the plugin tests. continue elif op.par_domain == 'N': params = np.asarray(np.random.random([op.num_params]), dtype=np.int64) else: params = np.random.random([op.num_params]) queue = [ op(*params, wires=list(range(op.num_wires)), do_queue=False) ] temp = isinstance(queue[0], qml.operation.CV) with self.assertRaisesRegex(qml.DeviceError, 'not supported on device'): if temp: expval = dev.execute( [qml.Rotation(0.5, wires=0, do_queue=False)], queue) else: expval = dev.execute( [qml.RX(0.5, wires=0, do_queue=False)], queue)
def circuit(*x): """Reference quantum function""" op(*x, wires=wires) return qml.expval(qml.PauliX(0))
def circuit_nn(dummy1, array, dummy2): qml.RY(0.5 * array[0,1], wires=0) qml.RY(-0.5 * array[1,1], wires=0) qml.RY(array[1,0], wires=1) return qml.expval(qml.PauliX(0)), qml.expval(qml.PauliX(1)) # returns a 2-vector
def circuit_n1v(dummy1, array, dummy2): qml.RY(0.5 * array[0,1], wires=0) qml.RY(-0.5 * array[1,1], wires=0) return qml.expval(qml.PauliX(0)), # note the comma, returns a 1-vector
def circuit_n1s(dummy1, array, dummy2): qml.RY(0.5 * array[0,1], wires=0) qml.RY(-0.5 * array[1,1], wires=0) return qml.expval(qml.PauliX(0)) # returns a scalar
def qf(x): qml.RX(x, wires=[0]) qml.CNOT(wires=[0, 1]) return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.PauliX(0))
def circuit(x): qml.RX(x, wires=[0]) return qml.sample(qml.PauliZ(0), 1), qml.sample(qml.PauliX(1), 1)
def circuit(): for i, p in enumerate(bits_to_flip): if p == 1: qml.PauliX(wires=[i]) return qml.expval.PauliZ(0), qml.expval.PauliZ( 1), qml.expval.PauliZ(2), qml.expval.PauliZ(3)
def qf(x, y): qml.RX(np.pi / 4, wires=[0]) qml.Rot(y, x, 2 * x, wires=[0]) return qml.expval(qml.PauliX(0))
def circuit(): return qml.var(qml.PauliX(0))
def circuit(x): qml.PauliX(wires=0) qml.CRY(x, wires=[0, 1]) return qml.expval(qml.PauliZ(0))