Skip to content

Commit

Permalink
Updated qiskit qpu tests removing ibm_qasm_simulator and replacing wi…
Browse files Browse the repository at this point in the history
…th ibm_kyoto when possible
  • Loading branch information
KilianPoirier committed Jun 3, 2024
1 parent 755fa6e commit dc9d685
Showing 1 changed file with 77 additions and 75 deletions.
152 changes: 77 additions & 75 deletions src/openqaoa-qiskit/tests/test_qpu_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ def test_circuit_angle_assignment_qpu_backend(self):

nqubits = 3
p = 2
weights = [1, 1, 1]
weights = [1, 1]
gammas = [0, 1 / 8 * np.pi]
betas = [1 / 2 * np.pi, 3 / 8 * np.pi]
shots = 1

cost_hamil = Hamiltonian(
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2)), PauliOp("ZZ", (0, 2))],
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2))],
weights,
1,
)
Expand All @@ -101,6 +101,7 @@ def test_circuit_angle_assignment_qpu_backend(self):
qaoa_descriptor, qiskit_device, shots, None, None, False
)
qpu_circuit = qiskit_backend.qaoa_circuit(variate_params)
qpu_circuit.draw()

# Standard Decomposition
main_circuit = QuantumCircuit(3)
Expand All @@ -110,9 +111,9 @@ def test_circuit_angle_assignment_qpu_backend(self):
main_circuit.cx(1, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(1, 2)
main_circuit.cx(0, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(0, 2)
# main_circuit.cx(0, 2)
# main_circuit.rz(2 * gammas[0], 2)
# main_circuit.cx(0, 2)
main_circuit.rx(-2 * betas[0], 0)
main_circuit.rx(-2 * betas[0], 1)
main_circuit.rx(-2 * betas[0], 2)
Expand All @@ -122,9 +123,9 @@ def test_circuit_angle_assignment_qpu_backend(self):
main_circuit.cx(1, 2)
main_circuit.rz(2 * gammas[1], 2)
main_circuit.cx(1, 2)
main_circuit.cx(0, 2)
main_circuit.rz(2 * gammas[1], 2)
main_circuit.cx(0, 2)
# main_circuit.cx(0, 2)
# main_circuit.rz(2 * gammas[1], 2)
# main_circuit.cx(0, 2)
main_circuit.rx(-2 * betas[1], 0)
main_circuit.rx(-2 * betas[1], 1)
main_circuit.rx(-2 * betas[1], 2)
Expand All @@ -144,13 +145,13 @@ def test_circuit_angle_assignment_qpu_backend_w_hadamard(self):

nqubits = 3
p = 2
weights = [1, 1, 1]
weights = [1, 1]
gammas = [0, 1 / 8 * np.pi]
betas = [1 / 2 * np.pi, 3 / 8 * np.pi]
shots = 1

cost_hamil = Hamiltonian(
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2)), PauliOp("ZZ", (0, 2))],
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2))],
weights,
1,
)
Expand All @@ -173,9 +174,9 @@ def test_circuit_angle_assignment_qpu_backend_w_hadamard(self):
main_circuit.cx(0, 1)
main_circuit.rz(2 * gammas[0], 1)
main_circuit.cx(0, 1)
main_circuit.cx(1, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(1, 2)
# main_circuit.cx(1, 2)
# main_circuit.rz(2 * gammas[0], 2)
# main_circuit.cx(1, 2)
main_circuit.cx(0, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(0, 2)
Expand All @@ -188,9 +189,9 @@ def test_circuit_angle_assignment_qpu_backend_w_hadamard(self):
main_circuit.cx(1, 2)
main_circuit.rz(2 * gammas[1], 2)
main_circuit.cx(1, 2)
main_circuit.cx(0, 2)
main_circuit.rz(2 * gammas[1], 2)
main_circuit.cx(0, 2)
# main_circuit.cx(0, 2)
# main_circuit.rz(2 * gammas[1], 2)
# main_circuit.cx(0, 2)
main_circuit.rx(-2 * betas[1], 0)
main_circuit.rx(-2 * betas[1], 1)
main_circuit.rx(-2 * betas[1], 2)
Expand All @@ -210,7 +211,7 @@ def test_prepend_circuit(self):

nqubits = 3
p = 1
weights = [1, 1, 1]
weights = [1, 1]
gammas = [1 / 8 * np.pi]
betas = [1 / 8 * np.pi]
shots = 1
Expand All @@ -220,7 +221,7 @@ def test_prepend_circuit(self):
prepend_circuit.x([0, 1, 2])

cost_hamil = Hamiltonian(
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2)), PauliOp("ZZ", (0, 2))],
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2))],
weights,
1,
)
Expand All @@ -247,9 +248,9 @@ def test_prepend_circuit(self):
main_circuit.cx(1, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(1, 2)
main_circuit.cx(0, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(0, 2)
# main_circuit.cx(0, 2)
# main_circuit.rz(2 * gammas[0], 2)
# main_circuit.cx(0, 2)
main_circuit.rx(-2 * betas[0], 0)
main_circuit.rx(-2 * betas[0], 1)
main_circuit.rx(-2 * betas[0], 2)
Expand All @@ -270,7 +271,7 @@ def test_append_circuit(self):

nqubits = 3
p = 1
weights = [1, 1, 1]
weights = [1, 1]
gammas = [1 / 8 * np.pi]
betas = [1 / 8 * np.pi]
shots = 1
Expand All @@ -280,7 +281,7 @@ def test_append_circuit(self):
append_circuit.x([0, 1, 2])

cost_hamil = Hamiltonian(
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2)), PauliOp("ZZ", (0, 2))],
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2))],
weights,
1,
)
Expand All @@ -306,9 +307,9 @@ def test_append_circuit(self):
main_circuit.cx(1, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(1, 2)
main_circuit.cx(0, 2)
main_circuit.rz(2 * gammas[0], 2)
main_circuit.cx(0, 2)
# main_circuit.cx(0, 2)
# main_circuit.rz(2 * gammas[0], 2)
# main_circuit.cx(0, 2)
main_circuit.rx(-2 * betas[0], 0)
main_circuit.rx(-2 * betas[0], 1)
main_circuit.rx(-2 * betas[0], 2)
Expand Down Expand Up @@ -390,55 +391,56 @@ def test_expectations_in_init(self):
True,
)

@pytest.mark.sim
def test_remote_integration_sim_run(self):
"""
Checks if Remote IBM QASM Simulator is similar/close to Local IBM
Statevector Simulator.
This test also serves as an integration test for the IBMQPU Backend.
This test takes a long time to complete.
"""

nqubits = 3
p = 1
weights = [1, 1, 1]
gammas = [[0], [1 / 8 * np.pi], [0], [1 / 8 * np.pi]]
betas = [[0], [0], [1 / 8 * np.pi], [1 / 8 * np.pi]]
shots = 10000

for i in range(4):
cost_hamil = Hamiltonian(
[PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2)), PauliOp("ZZ", (0, 2))],
weights,
1,
)
mixer_hamil = X_mixer_hamiltonian(n_qubits=nqubits)
qaoa_descriptor = QAOADescriptor(cost_hamil, mixer_hamil, p=p)
variate_params = QAOAVariationalStandardParams(
qaoa_descriptor, betas[i], gammas[i]
)

qiskit_device = DeviceQiskit(
"ibmq_qasm_simulator", self.HUB, self.GROUP, self.PROJECT
)

qiskit_backend = QAOAQiskitQPUBackend(
qaoa_descriptor, qiskit_device, shots, None, None, False
)
qiskit_expectation = qiskit_backend.expectation(variate_params)

qiskit_statevec_backend = QAOAQiskitBackendStatevecSimulator(
qaoa_descriptor, None, None, False
)
qiskit_statevec_expectation = qiskit_statevec_backend.expectation(
variate_params
)

acceptable_delta = 0.05 * qiskit_statevec_expectation
self.assertAlmostEqual(
qiskit_expectation, qiskit_statevec_expectation, delta=acceptable_delta
)
# Removed after removal of ibm_qasm_simulator backend
# @pytest.mark.sim
# def test_remote_integration_sim_run(self):
# """
# Checks if Remote IBM QASM Simulator is similar/close to Local IBM
# Statevector Simulator.
# This test also serves as an integration test for the IBMQPU Backend.

# This test takes a long time to complete.
# """

# nqubits = 3
# p = 1
# weights = [1, 1]
# gammas = [[0], [1 / 8 * np.pi], [0], [1 / 8 * np.pi]]
# betas = [[0], [0], [1 / 8 * np.pi], [1 / 8 * np.pi]]
# shots = 1

# for i in range(4):
# cost_hamil = Hamiltonian(
# [PauliOp("ZZ", (0, 1)), PauliOp("ZZ", (1, 2))],
# weights,
# 1,
# )
# mixer_hamil = X_mixer_hamiltonian(n_qubits=nqubits)
# qaoa_descriptor = QAOADescriptor(cost_hamil, mixer_hamil, p=p)
# variate_params = QAOAVariationalStandardParams(
# qaoa_descriptor, betas[i], gammas[i]
# )

# qiskit_device = DeviceQiskit(
# "ibmq_qasm_simulator", self.HUB, self.GROUP, self.PROJECT
# )

# qiskit_backend = QAOAQiskitQPUBackend(
# qaoa_descriptor, qiskit_device, shots, None, None, False
# )
# qiskit_expectation = qiskit_backend.expectation(variate_params)

# qiskit_statevec_backend = QAOAQiskitBackendStatevecSimulator(
# qaoa_descriptor, None, None, False
# )
# qiskit_statevec_expectation = qiskit_statevec_backend.expectation(
# variate_params
# )

# acceptable_delta = 0.05 * qiskit_statevec_expectation
# self.assertAlmostEqual(
# qiskit_expectation, qiskit_statevec_expectation, delta=acceptable_delta
# )

@pytest.mark.api
def test_remote_qubit_overflow(self):
Expand Down

0 comments on commit dc9d685

Please sign in to comment.