def test_do_queue(): """Assert do_queue=False is not queued""" with qml.tape.QuantumTape() as tape: qml.GroverOperator(wires=(0, 1), do_queue=False) assert len(tape.operations) == 0
def GroverSearch(num_iterations=1): for wire in wires: qml.Hadamard(wire) for _ in range(num_iterations): oracle() qml.GroverOperator(wires=wires) return qml.probs(wires)
def circ(): for wire in wires: qml.Hadamard(wire) for _ in range(5): qml.Hadamard(wires[0]) qml.MultiControlledX(wires=wires[0], control_wires=wires[1:]) qml.Hadamard(wires[0]) qml.GroverOperator(wires=wires) return qml.probs(wires=wires)
def test_expand(wires): """Asserts decomposition uses expected operations and wires""" op = qml.GroverOperator(wires=wires) decomp = op.expand().operations expected_wires = decomposition_wires(wires) for actual_op, expected_class, expected_wires in zip(decomp, decomp_3wires, expected_wires): assert isinstance(actual_op, expected_class) assert actual_op.wires == qml.wires.Wires(expected_wires)
def test_work_wires(): """Assert work wires get passed to MultiControlledX""" wires = ("a", "b") work_wire = ("aux",) op = qml.GroverOperator(wires=wires, work_wires=work_wire) assert op.work_wires == work_wire ops = op.expand().operations assert ops[2]._work_wires == work_wire
def test_grover_diffusion_matrix_results(): """Test that the matrix gives the same result as when running the example in the documentation `here <https://pennylane.readthedocs.io/en/stable/code/api/pennylane.templates.subroutines.GroverOperator.html>`_""" n_wires = 3 wires = list(range(n_wires)) def oracle(): qml.Hadamard(wires[-1]) qml.Toffoli(wires=wires) qml.Hadamard(wires[-1]) dev = qml.device("default.qubit", wires=wires) @qml.qnode(dev) def GroverSearch(num_iterations=1): for wire in wires: qml.Hadamard(wire) for _ in range(num_iterations): oracle() qml.GroverOperator(wires=wires) return qml.probs(wires) # Get probabilities from example probs_example = GroverSearch(num_iterations=1) # Grover diffusion matrix G_matrix = qml.GroverOperator(wires=wires).matrix oracle_matrix = np.identity(2 ** n_wires) oracle_matrix[-1, -1] = -1 # s1 = H|0>, Hadamard on a single qubit in the ground state s1 = np.array([1, 1]) / np.sqrt(2) # uniform superposition state s = functools.reduce(np.kron, list(itertools.repeat(s1, n_wires))) amplitudes = G_matrix @ oracle_matrix @ s # Check that the probabilities are the same probs_matrix = amplitudes ** 2 assert np.allclose(probs_example, probs_matrix)
def test_grover_diffusion_matrix(n_wires): """Test that the Grover diffusion matrix is the same as when constructed in a different way""" wires = list(range(n_wires)) # Test-oracle oracle = np.identity(2 ** n_wires) oracle[0, 0] = -1 # s1 = H|0>, Hadamard on a single qubit in the ground state s1 = np.array([1, 1]) / np.sqrt(2) # uniform superposition state s = functools.reduce(np.kron, list(itertools.repeat(s1, n_wires))) # Grover matrix G_matrix = qml.GroverOperator(wires=wires).matrix amplitudes = G_matrix @ oracle @ s probs = amplitudes ** 2 # Create Grover diffusion matrix G in alternative way oplist = list(itertools.repeat(Hadamard.matrix, n_wires - 1)) oplist.append(PauliZ.matrix) ctrl_str = "0" * (n_wires - 1) CX = MultiControlledX( control_values=ctrl_str, control_wires=wires[:-1], wires=wires[-1], work_wires=None, ).matrix M = functools.reduce(np.kron, oplist) G = M @ CX @ M amplitudes2 = G @ oracle @ s probs2 = amplitudes2 ** 2 assert np.allclose(probs, probs2)
assert ax.texts[2].get_text() == "RX\n(1.23)" plt.close() general_op_data = [ qml.RX(1.234, wires=0), qml.Hadamard(0), qml.S(wires=0), qml.IsingXX(1.234, wires=(0, 1)), qml.U3(1.234, 2.345, 3.456, wires=0), # State Prep qml.BasisState([0, 1, 0], wires=(0, 1, 2)), ### Templates qml.QFT(wires=range(3)), qml.Permute([4, 2, 0, 1, 3], wires=(0, 1, 2, 3, 4)), qml.GroverOperator(wires=(0, 1, 2, 3, 4, 5)), ### Continuous Variable qml.Kerr(1.234, wires=0), qml.Beamsplitter(1.234, 2.345, wires=(0, 1)), qml.Rotation(1.234, wires=0), ] class TestGeneralOperations: """Tests general operations.""" width = 0.75 - 2 * 0.2 @pytest.mark.parametrize("op", general_op_data) def test_general_operations(self, op): """Test that a variety of operations produce a rectangle across relevant wires
def test_id(): """Assert id keyword works""" op = qml.GroverOperator(wires=(0, 1), id="hello") assert op.id == "hello"
def test_single_wire_error(bad_wires): """Assert error raised when called with only a single wire""" with pytest.raises(ValueError, match="GroverOperator must have at least"): op = qml.GroverOperator(wires=bad_wires)