def decomposition(phi, wires): return [ qml.Hadamard(wires=[wires[0]]), qml.CY(wires=wires), qml.RY(phi / 2, wires=[wires[0]]), qml.RX(-phi / 2, wires=[wires[1]]), qml.CY(wires=wires), qml.Hadamard(wires=[wires[0]]), ]
def test_control_sanity_check(): """Test that control works on a very standard usecase.""" def make_ops(): qml.RX(0.123, wires=0) qml.RY(0.456, wires=2) qml.RX(0.789, wires=0) qml.Rot(0.111, 0.222, 0.333, wires=2), qml.PauliX(wires=2) qml.PauliY(wires=4) qml.PauliZ(wires=0) with QuantumTape() as tape: cmake_ops = ctrl(make_ops, control=1) #Execute controlled version. cmake_ops() expected = [ qml.CRX(0.123, wires=[1, 0]), qml.CRY(0.456, wires=[1, 2]), qml.CRX(0.789, wires=[1, 0]), qml.CRot(0.111, 0.222, 0.333, wires=[1, 2]), qml.CNOT(wires=[1, 2]), qml.CY(wires=[1, 4]), qml.CZ(wires=[1, 0]), ] assert len(tape.operations) == 1 ctrl_op = tape.operations[0] assert isinstance(ctrl_op, ControlledOperation) expanded = ctrl_op.expand() assert_equal_operations(expanded.operations, expected)
def parameterized_qubit_tape(): """A parametrized qubit ciruit.""" a, b, c = 0.1, 0.2, 0.3 angles = np.array([0.4, 0.5, 0.6]) with qml.tape.QuantumTape() as tape: qml.RX(a, wires=0) qml.RX(b, wires=1) qml.PauliZ(1) qml.CNOT(wires=[0, 1]).inv() qml.CRY(b, wires=[3, 1]) qml.RX(angles[0], wires=0) qml.RX(4 * angles[1], wires=1) qml.PhaseShift(17 / 9 * c, wires=2) qml.RZ(b, wires=3) qml.RX(angles[2], wires=2).inv() qml.CRY(0.3589, wires=[3, 1]).inv() qml.CSWAP(wires=[4, 2, 1]).inv() qml.QubitUnitary(np.eye(2), wires=[2]) qml.ControlledQubitUnitary(np.eye(2), control_wires=[0, 1], wires=[2]) qml.MultiControlledX(control_wires=[0, 1, 2], wires=[3]) qml.Toffoli(wires=[0, 2, 1]) qml.CNOT(wires=[0, 2]) qml.PauliZ(wires=[1]) qml.PauliZ(wires=[1]).inv() qml.CZ(wires=[0, 1]) qml.CZ(wires=[0, 2]).inv() qml.CY(wires=[1, 2]) qml.CY(wires=[2, 0]).inv() qml.CNOT(wires=[2, 1]) qml.CNOT(wires=[0, 2]) qml.SWAP(wires=[0, 2]).inv() qml.CNOT(wires=[1, 3]) qml.RZ(b, wires=3) qml.CSWAP(wires=[4, 0, 1]) qml.expval(qml.PauliY(0)), qml.var(qml.Hadamard(wires=1)), qml.sample(qml.PauliX(2)), qml.expval(qml.Hermitian(np.eye(4), wires=[3, 4])), return tape
def qfunc(x, y, z): qml.Hadamard(wires=wires[0]) qml.RZ(z, wires=wires[2]) qml.CNOT(wires=[wires[2], wires[1]]) qml.CNOT(wires=[wires[1], wires[0]]) qml.RX(x, wires=wires[0]) qml.CNOT(wires=[wires[1], wires[0]]) qml.RZ(-z, wires=wires[2]) qml.RX(y, wires=wires[0]) qml.PauliY(wires=wires[2]) qml.CY(wires=[wires[1], wires[2]]) return qml.expval(qml.PauliZ(wires=wires[0]))
def op(op_name): ops_list = { "RX": qml.RX(0.123, wires=0), "RY": qml.RY(1.434, wires=0), "RZ": qml.RZ(2.774, wires=0), "S": qml.S(wires=0), "SX": qml.SX(wires=0), "T": qml.T(wires=0), "CNOT": qml.CNOT(wires=[0, 1]), "CZ": qml.CZ(wires=[0, 1]), "CY": qml.CY(wires=[0, 1]), "SWAP": qml.SWAP(wires=[0, 1]), "ISWAP": qml.ISWAP(wires=[0, 1]), "SISWAP": qml.SISWAP(wires=[0, 1]), "SQISW": qml.SQISW(wires=[0, 1]), "CSWAP": qml.CSWAP(wires=[0, 1, 2]), "PauliRot": qml.PauliRot(0.123, "Y", wires=0), "IsingXX": qml.IsingXX(0.123, wires=[0, 1]), "IsingXY": qml.IsingXY(0.123, wires=[0, 1]), "IsingYY": qml.IsingYY(0.123, wires=[0, 1]), "IsingZZ": qml.IsingZZ(0.123, wires=[0, 1]), "Identity": qml.Identity(wires=0), "Rot": qml.Rot(0.123, 0.456, 0.789, wires=0), "Toffoli": qml.Toffoli(wires=[0, 1, 2]), "PhaseShift": qml.PhaseShift(2.133, wires=0), "ControlledPhaseShift": qml.ControlledPhaseShift(1.777, wires=[0, 2]), "CPhase": qml.CPhase(1.777, wires=[0, 2]), "MultiRZ": qml.MultiRZ(0.112, wires=[1, 2, 3]), "CRX": qml.CRX(0.836, wires=[2, 3]), "CRY": qml.CRY(0.721, wires=[2, 3]), "CRZ": qml.CRZ(0.554, wires=[2, 3]), "Hadamard": qml.Hadamard(wires=0), "PauliX": qml.PauliX(wires=0), "PauliY": qml.PauliY(wires=0), "PauliZ": qml.PauliZ(wires=0), "CRot": qml.CRot(0.123, 0.456, 0.789, wires=[0, 1]), "DiagonalQubitUnitary": qml.DiagonalQubitUnitary(np.array([1.0, 1.0j]), wires=1), "ControlledQubitUnitary": qml.ControlledQubitUnitary( np.eye(2) * 1j, wires=[0], control_wires=[2] ), "MultiControlledX": qml.MultiControlledX(wires=(0, 1, 2), control_values="01"), "SingleExcitation": qml.SingleExcitation(0.123, wires=[0, 3]), "SingleExcitationPlus": qml.SingleExcitationPlus(0.123, wires=[0, 3]), "SingleExcitationMinus": qml.SingleExcitationMinus(0.123, wires=[0, 3]), "DoubleExcitation": qml.DoubleExcitation(0.123, wires=[0, 1, 2, 3]), "DoubleExcitationPlus": qml.DoubleExcitationPlus(0.123, wires=[0, 1, 2, 3]), "DoubleExcitationMinus": qml.DoubleExcitationMinus(0.123, wires=[0, 1, 2, 3]), "QFT": qml.QFT(wires=0), "QubitSum": qml.QubitSum(wires=[0, 1, 2]), "QubitCarry": qml.QubitCarry(wires=[0, 1, 2, 3]), "QubitUnitary": qml.QubitUnitary(np.eye(2) * 1j, wires=0), } return ops_list.get(op_name)
def test_CY_decomposition(self, tol): """Tests that the decomposition of the CY gate is correct""" op = qml.CY(wires=[0, 1]) res = op.decomposition(op.wires) mats = [] for i in reversed(res): if len(i.wires) == 1: mats.append(np.kron(i.matrix, np.eye(2))) else: mats.append(i.matrix) decomposed_matrix = np.linalg.multi_dot(mats) assert np.allclose(decomposed_matrix, op.matrix, atol=tol, rtol=0)
np.random.seed(42) # ========================================================== # Some useful global variables # gates for which device support is tested ops = { "BasisState": qml.BasisState(np.array([0]), wires=[0]), "CNOT": qml.CNOT(wires=[0, 1]), "CRX": qml.CRX(0, wires=[0, 1]), "CRY": qml.CRY(0, wires=[0, 1]), "CRZ": qml.CRZ(0, wires=[0, 1]), "CRot": qml.CRot(0, 0, 0, wires=[0, 1]), "CSWAP": qml.CSWAP(wires=[0, 1, 2]), "CZ": qml.CZ(wires=[0, 1]), "CY": qml.CY(wires=[0, 1]), "DiagonalQubitUnitary": qml.DiagonalQubitUnitary(np.array([1, 1]), wires=[0]), "Hadamard": qml.Hadamard(wires=[0]), "MultiRZ": qml.MultiRZ(0, wires=[0]), "PauliX": qml.PauliX(wires=[0]), "PauliY": qml.PauliY(wires=[0]), "PauliZ": qml.PauliZ(wires=[0]), "PhaseShift": qml.PhaseShift(0, wires=[0]), "ControlledPhaseShift": qml.ControlledPhaseShift(0, wires=[0, 1]), "QubitStateVector": qml.QubitStateVector(np.array([1.0, 0.0]), wires=[0]), "QubitUnitary": qml.QubitUnitary(np.eye(2), wires=[0]), "ControlledQubitUnitary": qml.ControlledQubitUnitary(np.eye(2), control_wires=[1], wires=[0]), "MultiControlledX": qml.MultiControlledX(control_wires=[1, 2], wires=[0]), "RX": qml.RX(0, wires=[0]), "RY": qml.RY(0, wires=[0]), "RZ": qml.RZ(0, wires=[0]),
def qfunc(): qml.CRY(-0.2, wires=["a", 2]) qml.PauliY(wires="a") qml.CNOT(wires=[1, 2]) qml.CY(wires=["a", 1]) qml.RY(0.3, wires="a")
"CNOT": qml.CNOT(wires=[0, 1]), "CRX": qml.CRX(0, wires=[0, 1]), "CRY": qml.CRY(0, wires=[0, 1]), "CRZ": qml.CRZ(0, wires=[0, 1]), "CRot": qml.CRot(0, 0, 0, wires=[0, 1]), "CSWAP": qml.CSWAP(wires=[0, 1, 2]), "CZ": qml.CZ(wires=[0, 1]), "CY": qml.CY(wires=[0, 1]), "DiagonalQubitUnitary": qml.DiagonalQubitUnitary(np.array([1, 1]), wires=[0]), "Hadamard": qml.Hadamard(wires=[0]), "MultiRZ": qml.MultiRZ(0, wires=[0]), "PauliX": qml.PauliX(wires=[0]), "PauliY": qml.PauliY(wires=[0]), "PauliZ": qml.PauliZ(wires=[0]), "PhaseShift": qml.PhaseShift(0, wires=[0]), "ControlledPhaseShift":
def decomposition(phi, wires): return [ qml.CY(wires=wires), qml.RY(phi, wires=[wires[0]]), qml.CY(wires=wires), ]
"""Test WireCut gets correct special call.""" with QuantumTape() as tape: qml.WireCut(wires=(0, 1)) _, ax = tape_mpl(tape) layer = 0 assert len(ax.lines) == 2 assert len(ax.collections) == 2 plt.close() controlled_data = [ (qml.CY(wires=(0, 1)), "Y"), (qml.CRX(1.2345, wires=(0, 1)), "RX"), (qml.CRot(1.2, 2.2, 3.3, wires=(0, 1)), "Rot"), ] class TestControlledGates: """Tests generic controlled gates""" width = 0.75 - 2 * 0.2 @pytest.mark.parametrize("op, label", controlled_data) def test_control_gates(self, op, label): """Test a variety of non-special gates. Checks control wires are drawn, and that a box is drawn over the target wires."""
assert np.allclose(f(), rnd_state) spy.assert_called() label_data = [ (qml.Identity(0), "I", "I"), (qml.Hadamard(0), "H", "H"), (qml.PauliX(0), "X", "X"), (qml.PauliY(0), "Y", "Y"), (qml.PauliZ(0), "Z", "Z"), (qml.S(wires=0), "S", "S⁻¹"), (qml.T(wires=0), "T", "T⁻¹"), (qml.SX(wires=0), "SX", "SX⁻¹"), (qml.CNOT(wires=(0, 1)), "⊕", "⊕"), (qml.CZ(wires=(0, 1)), "Z", "Z"), (qml.CY(wires=(0, 1)), "Y", "Y"), (qml.SWAP(wires=(0, 1)), "SWAP", "SWAP⁻¹"), (qml.ISWAP(wires=(0, 1)), "ISWAP", "ISWAP⁻¹"), (qml.SISWAP(wires=(0, 1)), "SISWAP", "SISWAP⁻¹"), (qml.SQISW(wires=(0, 1)), "SISWAP", "SISWAP⁻¹"), (qml.CSWAP(wires=(0, 1, 2)), "SWAP", "SWAP"), (qml.Toffoli(wires=(0, 1, 2)), "⊕", "⊕"), (qml.MultiControlledX(control_wires=(0, 1, 2), wires=(3)), "⊕", "⊕"), (qml.Barrier(0), "||", "||"), (qml.WireCut(wires=0), "//", "//"), ] @pytest.mark.parametrize("op, label1, label2", label_data) def test_label_method(op, label1, label2): assert op.label() == label1