def qf(x, y): qml.Displacement(x, 0, wires=[0]) qml.CubicPhase(0.2, wires=[0]) # nongaussian succeeding x qml.Squeezing(0.3, x, wires=[1]) # x affects gates on both wires, y unused qml.Rotation(1.3, wires=[1]) return qml.expval(qml.X(0)), qml.expval(qml.X(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 qf(x, y): qml.Displacement(x, 0, wires=[0]) qml.Displacement(1.2, y, wires=[0]) qml.Beamsplitter(0.2, 1.7, wires=[0, 1]) qml.Rotation(1.9, wires=[0]) qml.Kerr(0.3, wires=[1]) # nongaussian succeeding both x and y due to the beamsplitter return qml.expval(qml.X(0)), qml.expval(qml.X(1))
def qf(x, y): qml.Displacement(x, 0, wires=[0]) qml.CubicPhase(0.2, wires=[0]) qml.Squeezing(0.3, y, wires=[1]) qml.Rotation(1.3, wires=[1]) # nongaussian succeeding x but not y # TODO when QNode uses a DAG to describe the circuit, uncomment this line #qml.Kerr(0.4, [0]) return qml.expval(qml.X(0)), qml.expval(qml.X(1))
def circuit(*x): """Test quantum function""" x = prep_par(x, op) op(*x, wires=wires) if issubclass(op, qml.operation.CV): return qml.expval(qml.X(0)) return qml.expval(qml.PauliZ(0))
def circuit(x, c=None): qml.RX(x, wires=0) for i in range(c.val): qml.RX(x, wires=i) return qml.expval(qml.PauliZ(0))
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))
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 qf(x, y): qml.Displacement(0.5, 0, wires=[0]) qml.Squeezing(x, 0, wires=[0]) M = np.zeros((5, 5), dtype=object) M[1,1] = y M[1,2] = 1.0 M[2,1] = 1.0 return qml.expval(qml.PolyXP(M, [0, 1]))
def circuit(x): qml.Displacement(x, 0, wires=0) if cls.par_domain == 'A': cls(U, wires=w) else: cls(wires=w) return qml.expval(qml.X(0))
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 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(qml.PauliZ(0))
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(qml.PauliZ(0))
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)
def circuit(w): qml.RX(w[np.unravel_index(0, s)], wires=0) # b[0] qml.RX(w[np.unravel_index(1, s)], wires=1) # b[1] qml.RX(w[np.unravel_index(2, s)], wires=2) # ... qml.RX(w[np.unravel_index(3, s)], wires=3) qml.RX(w[np.unravel_index(4, s)], wires=4) qml.RX(w[np.unravel_index(5, s)], wires=5) qml.RX(w[np.unravel_index(6, s)], wires=6) qml.RX(w[np.unravel_index(7, s)], wires=7) return tuple(qml.expval(qml.PauliZ(idx)) for idx in range(len(b)))
def circuit(x): args = [0.3] * G.num_params args[0] = x qml.Displacement(0.5, 0, wires=0) G(*args, wires=range(G.num_wires)) qml.Beamsplitter(1.3, -2.3, wires=[0, 1]) qml.Displacement(-0.5, 0, wires=0) qml.Squeezing(0.5, -1.5, wires=0) qml.Rotation(-1.1, wires=0) return qml.expval(O(wires=0))
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 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(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 circuit(*x): """Reference quantum function""" qml.RX(a, wires=0) return qml.expval(op(*x, wires=wires))
def circuit(*x): """Reference quantum function""" op(*x, wires=wires) return qml.expval(qml.PauliX(0))
def circuit(x): """Test quantum function""" qml.RX(x, wires=0) return qml.expval(qml.PauliY(0))
def circuit(*x): """Test quantum function""" x = prep_par(x, op) return qml.expval(op(*x, wires=wires))
def circuit(): SomeOperation(wires=0) return qml.expval(qml.PauliZ(0))
def qf(x): qml.RX(x, wires=[0]) ev = qml.expval(qml.PauliZ(1)) qml.RY(0.5, wires=[0]) return ev
def qf(x): qml.RX(x, wires=[0]) ex = qml.expval(qml.PauliZ(1)) return qml.expval(qml.PauliZ(0)), ex
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(): return qml.expval(SomeObservable( wires=0)) #this expectation will never be supported
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 qf(x): qml.RX(x, wires=[0]) return qml.expval(qml.PauliZ(0)), 0.3