def decomposition(theta, wires): decomp_ops = [ qml.PauliX(wires=wires[0]), qml.PauliX(wires=wires[1]), qml.ControlledPhaseShift(theta / 2, wires=[wires[1], wires[0]]), qml.PauliX(wires=wires[0]), qml.PauliX(wires=wires[1]), qml.ControlledPhaseShift(theta / 2, wires=[wires[0], wires[1]]), qml.CNOT(wires=[wires[0], wires[1]]), qml.CRY(theta, wires=[wires[1], wires[0]]), qml.CNOT(wires=[wires[0], wires[1]]), ] return decomp_ops
def circuit(): qml.CRZ(0.0, wires=[0, 1]) qml.CRX(0.0, wires=[0, 1]) qml.PhaseShift(0.0, wires=0) qml.ControlledPhaseShift(0.0, wires=[1, 0]) qml.CRot(1.0, 0.0, 0.0, wires=[0, 1]) qml.CRY(0.0, wires=[0, 1]) return qml.sample(qml.PauliZ(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)
class TestHelpers: @pytest.mark.parametrize( "spectrum1, spectrum2, expected", [ ([-1, 0, 1], [-1, 0, 1], [-2, -1, 0, 1, 2]), ([-3, 0, 3], [-5, 0, 5], [-8, -5, -3, -2, 0, 2, 3, 5, 8]), ([-2, -1, 0, 1, 2], [-1, 0, 1], [-3, -2, -1, 0, 1, 2, 3]), ([-0.5, 0, 0.5], [-1, 0, 1], [-1.5, -1, -0.5, 0, 0.5, 1.0, 1.5]), ([-0.5, 0, 0.5], [], [-0.5, 0, 0.5]), ([], [-0.5, 0, 0.5], [-0.5, 0, 0.5]), ], ) def test_join_spectra(self, spectrum1, spectrum2, expected, tol): """Test that spectra are joined correctly.""" joined = _join_spectra(spectrum1, spectrum2) assert np.allclose(joined, expected, atol=tol, rtol=0) @pytest.mark.parametrize( "op, expected", [ (qml.RX(0.1, wires=0), [-1, 0, 1]), # generator is a class (qml.RY(0.1, wires=0), [-1, 0, 1]), # generator is a class (qml.RZ(0.1, wires=0), [-1, 0, 1]), # generator is a class (qml.PhaseShift(0.5, wires=0), [-1, 0, 1]), # generator is an array (qml.ControlledPhaseShift(0.5, wires=[0, 1]), [-1, 0, 1]), # generator is an array ], ) def test_get_spectrum(self, op, expected, tol): """Test that the spectrum is correctly extracted from an operator.""" spec = _get_spectrum(op) assert np.allclose(spec, expected, atol=tol, rtol=0) def test_get_spectrum_complains_no_generator(self): """Test that an error is raised if the operator has no generator defined.""" # Observables have no generator attribute with pytest.raises(ValueError, match="Generator of operation"): _get_spectrum(qml.P(wires=0)) # CNOT is an operation where generator is an abstract property with pytest.raises(ValueError, match="Generator of operation"): _get_spectrum(qml.CNOT(wires=[0, 1]))
def decomposition(wires): num_wires = len(wires) shifts = [2 * np.pi * 2**-i for i in range(2, num_wires + 1)] decomp_ops = [] for i, wire in enumerate(wires): decomp_ops.append(qml.Hadamard(wire)) for shift, control_wire in zip(shifts[:len(shifts) - i], wires[i + 1:]): op = qml.ControlledPhaseShift(shift, wires=[control_wire, wire]) decomp_ops.append(op) first_half_wires = wires[:num_wires // 2] last_half_wires = wires[-(num_wires // 2):] for wire1, wire2 in zip(first_half_wires, reversed(last_half_wires)): swap = qml.SWAP(wires=[wire1, wire2]) decomp_ops.append(swap) return decomp_ops
"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]), "Rot": qml.Rot(0, 0, 0, wires=[0]), "S": qml.S(wires=[0]), "SWAP": qml.SWAP(wires=[0, 1]), "T": qml.T(wires=[0]), "SX": qml.SX(wires=[0]), "Toffoli": qml.Toffoli(wires=[0, 1, 2]), "QFT": qml.QFT(wires=[0, 1, 2]), "SingleExcitation": qml.SingleExcitation(0, wires=[0, 1]),
) def test_join_spectra(spectrum1, spectrum2, expected): """Test that spectra are joined correctly.""" joined = join_spectra(spectrum1, spectrum2) assert joined == expected @pytest.mark.parametrize( "op, expected", [ (qml.RX(0.1, wires=0), [0, 1]), # generator is a class (qml.RY(0.1, wires=0), [0, 1]), # generator is a class (qml.RZ(0.1, wires=0), [0, 1]), # generator is a class (qml.PhaseShift(0.5, wires=0), [0, 1]), # generator is an array (qml.CRX(0.2, wires=[0, 1]), [0, 0.5, 1]), # generator is an array (qml.ControlledPhaseShift( 0.5, wires=[0, 1]), [0, 1]), # generator is an array ], ) def test_get_spectrum(op, expected): """Test that the spectrum is correctly extracted from an operator.""" spec = get_spectrum(op, decimals=10) assert np.allclose(sorted(spec), expected, atol=1e-6, rtol=0) def test_get_spectrum_complains_no_generator(): """Test that an error is raised if the operator has no generator defined.""" # Observables have no generator attribute with pytest.raises(ValueError, match="Generator of operation"): get_spectrum(qml.P(wires=0), decimals=10)