def result(): qml.PauliX(0) qml.PauliX(2) qml.SingleExcitation(0.1, wires=[0, 1]) qml.SingleExcitation(0.2, wires=[2, 3]) qml.SingleExcitation(0.3, wires=[1, 2]) return qml.expval(qml.SparseHamiltonian(h, wires=[0, 1, 2, 3]))
def circuit(angles): """Prepares the quantum state in the problem statement and returns qml.probs Args: - angles (list(float)): The relevant angles in the problem statement in this order: [alpha, beta, gamma] Returns: - (np.tensor): The probability of each computational basis state """ # QHACK # # Prepare -|001011> qml.PauliX(wires=2) qml.PauliX(wires=4) qml.PauliX(wires=5) qml.PauliZ(wires=2) # Exchange excitations qml.SingleExcitation(angles[0], wires=(1, 4)) # Rotate |001011> -> |011001> qml.DoubleExcitation(3 * np.pi + angles[1], wires=(0, 1, 4, 5)) # Rotate |001011> -> |111000> qml.QubitUnitary(triple_excitation_matrix(angles[2]), wires=(0, 1, 2, 3, 4, 5)) # Rotate |111000> -> |000111> # QHACK # return qml.probs(wires=range(NUM_WIRES))
def test_single_excitation_generator(self, phi): """Tests that the SingleExcitation operation calculates the correct generator""" op = qml.SingleExcitation(phi, wires=[0, 1]) g, a = op.generator res = expm(1j * a * g * phi) exp = SingleExcitation(phi) assert np.allclose(res, exp)
def test_single_excitation_decomp(self, phi): """Tests that the SingleExcitation operation calculates the correct decomposition. Need to consider the matrix of CRY separately, as the control is wire 1 and the target is wire 0 in the decomposition.""" decomp = qml.SingleExcitation(phi, wires=[0, 1]).decompose() mats = [] for i in reversed(decomp): if i.wires.tolist() == [1, 0] and isinstance(i, qml.CRY): new_mat = np.array([ [1, 0, 0, 0], [0, np.cos(phi / 2), 0, -np.sin(phi / 2)], [0, 0, 1, 0], [0, np.sin(phi / 2), 0, np.cos(phi / 2)], ]) mats.append(new_mat) else: mats.append(i.matrix) decomposed_matrix = np.linalg.multi_dot(mats) exp = SingleExcitation(phi) assert np.allclose(decomposed_matrix, exp)
def test_custom_wires_circuit(self): """Test expected serialization for a simple circuit with custom wire labels""" wires_dict = {"a": 0, 3.2: 1} with qml.tape.QuantumTape() as tape: qml.RX(0.4, wires="a") qml.RY(0.6, wires=3.2) qml.CNOT(wires=["a", 3.2]) qml.SingleExcitation(0.5, wires=["a", 3.2]) qml.SingleExcitationPlus(0.4, wires=["a", 3.2]) qml.SingleExcitationMinus(0.5, wires=["a", 3.2]).inv() s = _serialize_ops(tape, wires_dict) s_expected = ( ( [ "RX", "RY", "CNOT", "SingleExcitation", "SingleExcitationPlus", "SingleExcitationMinus", ], [[0.4], [0.6], [], [0.5], [0.4], [0.5]], [[0], [1], [0, 1], [0, 1], [0, 1], [0, 1]], [False, False, False, False, False, True], [[], [], [], [], [], []], ), False, ) assert s == s_expected
def circuit_default(): qml.BasisState(hf_state, wires=range(qubits)) for i, excitation in enumerate(excitations): if len(excitation) == 4: qml.DoubleExcitation(1, wires=excitation) elif len(excitation) == 2: qml.SingleExcitation(1, wires=excitation) return qml.expval(qml.SparseHamiltonian(H_sparse, wires=wires))
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 expand(self): weights = self.parameters[0] with qml.tape.QuantumTape() as tape: BasisState(self.hf_state, wires=self.wires) for i, d_wires in enumerate(self.doubles): qml.DoubleExcitation(weights[len(self.singles) + i], wires=d_wires) for j, s_wires in enumerate(self.singles): qml.SingleExcitation(weights[j], wires=s_wires) return tape
def circuit_ansatz(params, wires): """Circuit ansatz containing all the parametrized gates""" qml.QubitStateVector(unitary_group.rvs(2**4, random_state=0)[0], wires=wires) qml.RX(params[0], wires=wires[0]) qml.RY(params[1], wires=wires[1]) qml.RX(params[2], wires=wires[2]).inv() qml.RZ(params[0], wires=wires[3]) qml.CRX(params[3], wires=[wires[3], wires[0]]) qml.PhaseShift(params[4], wires=wires[2]) qml.CRY(params[5], wires=[wires[2], wires[1]]) qml.CRZ(params[5], wires=[wires[0], wires[3]]).inv() qml.PhaseShift(params[6], wires=wires[0]).inv() qml.Rot(params[6], params[7], params[8], wires=wires[0]) # # qml.Rot(params[8], params[8], params[9], wires=wires[1]).inv() qml.MultiRZ(params[11], wires=[wires[0], wires[1]]) # # qml.PauliRot(params[12], "XXYZ", wires=[wires[0], wires[1], wires[2], wires[3]]) qml.CPhase(params[12], wires=[wires[3], wires[2]]) qml.IsingXX(params[13], wires=[wires[1], wires[0]]) qml.IsingXY(params[14], wires=[wires[3], wires[2]]) qml.IsingYY(params[14], wires=[wires[3], wires[2]]) qml.IsingZZ(params[14], wires=[wires[2], wires[1]]) qml.U1(params[15], wires=wires[0]) qml.U2(params[16], params[17], wires=wires[0]) qml.U3(params[18], params[19], params[20], wires=wires[1]) # # qml.CRot(params[21], params[22], params[23], wires=[wires[1], wires[2]]).inv() # expected tofail qml.SingleExcitation(params[24], wires=[wires[2], wires[0]]) qml.DoubleExcitation(params[25], wires=[wires[2], wires[0], wires[1], wires[3]]) qml.SingleExcitationPlus(params[26], wires=[wires[0], wires[2]]) qml.SingleExcitationMinus(params[27], wires=[wires[0], wires[2]]) qml.DoubleExcitationPlus(params[27], wires=[wires[2], wires[0], wires[1], wires[3]]) qml.DoubleExcitationMinus(params[27], wires=[wires[2], wires[0], wires[1], wires[3]]) qml.RX(params[28], wires=wires[0]) qml.RX(params[29], wires=wires[1])
"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]), "SingleExcitationPlus": qml.SingleExcitationPlus(0, wires=[0, 1]), "SingleExcitationMinus": qml.SingleExcitationMinus(0, wires=[0, 1]), "DoubleExcitation": qml.DoubleExcitation(0, wires=[0, 1, 2, 3]), "DoubleExcitationPlus": qml.DoubleExcitationPlus(0, wires=[0, 1, 2, 3]), "DoubleExcitationMinus": qml.DoubleExcitationMinus(0, wires=[0, 1, 2, 3]), "QubitCarry": qml.QubitCarry(wires=[0, 1, 2, 3]), "QubitSum:": qml.QubitSum(wires=[0, 1, 2]), } all_ops = ops.keys() # non-parametrized qubit gates I = np.identity(2) X = np.array([[0, 1], [1, 0]]) Y = np.array([[0, -1j], [1j, 0]])
def test_single_excitation_matrix(self, phi): """Tests that the SingleExcitation operation calculates the correct matrix""" op = qml.SingleExcitation(phi, wires=[0, 1]) res = op.matrix exp = SingleExcitation(phi) assert np.allclose(res, exp)
def circuit(phi): qml.PauliX(wires=0) qml.PauliX(wires=1) qml.OrbitalRotation(phi, wires=[0, 1, 2, 3]) return qml.expval(qml.PauliZ(0)) phi_t = torch.tensor(phi, dtype=torch.complex128, requires_grad=True) result = circuit(phi_t) result.backward() assert np.allclose(phi_t.grad, np.sin(phi)) label_data = [ (qml.SingleExcitation(1.2345, wires=(0, 1)), "G", "G\n(1.23)", "G\n(1)"), (qml.SingleExcitationMinus(1.2345, wires=(0, 1)), "G₋", "G₋\n(1.23)", "G₋\n(1)"), (qml.SingleExcitationPlus(1.2345, wires=(0, 1)), "G₊", "G₊\n(1.23)", "G₊\n(1)"), (qml.DoubleExcitation(2.3456, wires=(0, 1, 2, 3)), "G²", "G²\n(2.35)", "G²\n(2)"), (qml.DoubleExcitationPlus(2.3456, wires=(0, 1, 2, 3)), "G²₊", "G²₊\n(2.35)", "G²₊\n(2)"), (qml.DoubleExcitationMinus(2.345, wires=(0, 1, 2, 3)), "G²₋", "G²₋\n(2.35)", "G²₋\n(2)"), ( qml.OrbitalRotation(2.3456, wires=(0, 1, 2, 3)), "OrbitalRotation", "OrbitalRotation\n(2.35)", "OrbitalRotation\n(2)",
def circuit_decomposed(weights): qml.BasisState(np.array([1, 1, 0, 0]), wires=range(4)) qml.DoubleExcitation(weights[1], wires=[0, 1, 2, 3]) qml.SingleExcitation(weights[0], wires=[0, 1]) return qml.expval(qml.PauliZ(0))
def circuit(x): qml.SingleExcitation(x, wires=[0, 1]) return qml.expval(qml.PauliX(0))
def circuit(params, wires): qml.PauliX(0) qml.PauliX(1) qml.DoubleExcitation(params[0], wires=[0, 1, 2, 3]) qml.SingleExcitation(params[1], wires=[0, 2]) qml.SingleExcitation(params[2], wires=[1, 3])