This is an automated email from the ASF dual-hosted git repository.
rawkintrevo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/mahout.git
The following commit(s) were added to refs/heads/main by this push:
new 342d5e5d6 Add Cirq backend test (#581)
342d5e5d6 is described below
commit 342d5e5d6f1aa53cdebf937702347811f20d3f1b
Author: Guan Ming(Wesley) Chiu <[email protected]>
AuthorDate: Thu Oct 9 23:15:44 2025 +0800
Add Cirq backend test (#581)
---
qumat/cirq_backend.py | 6 ++++
testing/{qiskit_helpers.py => cirq_helpers.py} | 49 +++++++++++---------------
testing/conftest.py | 5 ++-
testing/qiskit_helpers.py | 6 ++--
testing/qumat_helpers.py | 7 ++--
testing/test_final_quantum_states.py | 9 +++--
6 files changed, 44 insertions(+), 38 deletions(-)
diff --git a/qumat/cirq_backend.py b/qumat/cirq_backend.py
index f0c24dd1c..c75e98b69 100644
--- a/qumat/cirq_backend.py
+++ b/qumat/cirq_backend.py
@@ -130,3 +130,9 @@ def apply_u_gate(circuit, qubit_index, theta, phi, lambd):
circuit.append(cirq.rz(lambd).on(qubit))
circuit.append(cirq.ry(phi).on(qubit))
circuit.append(cirq.rx(theta).on(qubit))
+
+
+def get_final_state_vector(circuit, backend, backend_config):
+ simulator = cirq.Simulator()
+ result = simulator.simulate(circuit)
+ return result.final_state_vector
diff --git a/testing/qiskit_helpers.py b/testing/cirq_helpers.py
similarity index 52%
copy from testing/qiskit_helpers.py
copy to testing/cirq_helpers.py
index fd40a9767..be8808324 100644
--- a/testing/qiskit_helpers.py
+++ b/testing/cirq_helpers.py
@@ -15,17 +15,15 @@
# limitations under the License.
#
-# Import necessary Qiskit libraries
-from qiskit import Aer, QuantumCircuit, transpile
-from qiskit.quantum_info import Statevector
+import cirq
+import numpy as np
def get_qumat_backend_config(test_type: str = "get_final_state_vector"):
if test_type == "get_final_state_vector":
- print("success")
qumat_backend_config = {
- "backend_name": "qiskit",
- "backend_options": {"simulator_type": "statevector_simulator",
"shots": 1},
+ "backend_name": "cirq",
+ "backend_options": {"simulator_type": "default", "shots": 1},
}
else:
pass
@@ -35,37 +33,32 @@ def get_qumat_backend_config(test_type: str =
"get_final_state_vector"):
def get_native_example_final_state_vector(
initial_state_ket_str: str = "000",
-) -> Statevector:
+) -> np.ndarray:
n_qubits = len(initial_state_ket_str)
- assert n_qubits == 3, print(
- "The current qiskit native testing example is strictly 3 qubits"
- )
+ assert n_qubits == 3, "The current cirq native testing example is strictly
3 qubits"
- simulator = Aer.get_backend("statevector_simulator")
+ qubits = cirq.LineQubit.range(n_qubits)
+ circuit = cirq.Circuit()
- qc = QuantumCircuit(n_qubits)
-
- initial_state = Statevector.from_label(initial_state_ket_str)
- qc.initialize(initial_state, range(n_qubits))
+ # Initialize to desired state
+ for i, bit in enumerate(initial_state_ket_str):
+ if bit == "1":
+ circuit.append(cirq.X(qubits[i]))
# Create entanglement between qubits 1 and 2
- qc.h(1) # Apply Hadamard gate on qubit 1
- qc.cx(1, 2) # Apply CNOT gate with qubit 1 as control and qubit 2 as
target
+ circuit.append(cirq.H(qubits[1]))
+ circuit.append(cirq.CNOT(qubits[1], qubits[2]))
# Prepare the state to be teleported on qubit 0
- qc.h(0) # Apply Hadamard gate on qubit 0
- qc.z(0) # Apply Pauli-Z gate on qubit 0
+ circuit.append(cirq.H(qubits[0]))
+ circuit.append(cirq.Z(qubits[0]))
# Perform Bell measurement on qubits 0 and 1
- qc.cx(0, 1) # Apply CNOT gate with qubit 0 as control and qubit 1 as
target
- qc.h(0) # Apply Hadamard gate on qubit 0
+ circuit.append(cirq.CNOT(qubits[0], qubits[1]))
+ circuit.append(cirq.H(qubits[0]))
# Simulate the circuit
- transpiled_qc = transpile(qc, simulator)
- job = simulator.run(transpiled_qc)
- result = job.result()
-
- # Get the state vector
- state_vector = result.get_statevector()
+ simulator = cirq.Simulator()
+ result = simulator.simulate(circuit)
- return state_vector
+ return result.final_state_vector
diff --git a/testing/conftest.py b/testing/conftest.py
index 39ce482d0..01f088f2f 100644
--- a/testing/conftest.py
+++ b/testing/conftest.py
@@ -23,8 +23,11 @@ from pathlib import Path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
+# Define backends to test - used by both parametrize and fixture
+TESTING_BACKENDS = ["qiskit", "cirq"] # Can be expanded to include "braket"
when ready
+
@pytest.fixture(scope="session")
def testing_backends():
"""Fixture to provide the list of backends to test."""
- return ["qiskit"] # Can be expanded to include "cirq", "braket" when ready
+ return TESTING_BACKENDS
diff --git a/testing/qiskit_helpers.py b/testing/qiskit_helpers.py
index fd40a9767..12863ff44 100644
--- a/testing/qiskit_helpers.py
+++ b/testing/qiskit_helpers.py
@@ -45,8 +45,10 @@ def get_native_example_final_state_vector(
qc = QuantumCircuit(n_qubits)
- initial_state = Statevector.from_label(initial_state_ket_str)
- qc.initialize(initial_state, range(n_qubits))
+ # Initialize state using X gates (backend-agnostic)
+ for i, bit in enumerate(initial_state_ket_str):
+ if bit == "1":
+ qc.x(i)
# Create entanglement between qubits 1 and 2
qc.h(1) # Apply Hadamard gate on qubit 1
diff --git a/testing/qumat_helpers.py b/testing/qumat_helpers.py
index c01bf6580..693b86005 100644
--- a/testing/qumat_helpers.py
+++ b/testing/qumat_helpers.py
@@ -51,8 +51,11 @@ def get_qumat_example_final_state_vector(
qumat_instance = QuMat(backend_config)
qumat_instance.create_empty_circuit(num_qubits=3)
- initial_state = create_np_computational_basis_state(initial_state_ket_str)
- qumat_instance.circuit.initialize(initial_state, range(n_qubits))
+
+ # Initialize state using X gates (backend-agnostic)
+ for i, bit in enumerate(initial_state_ket_str):
+ if bit == "1":
+ qumat_instance.apply_pauli_x_gate(qubit_index=i)
qumat_instance.apply_hadamard_gate(qubit_index=1)
qumat_instance.apply_cnot_gate(control_qubit_index=1, target_qubit_index=2)
diff --git a/testing/test_final_quantum_states.py
b/testing/test_final_quantum_states.py
index 0742e3f7a..b0b0e59e1 100644
--- a/testing/test_final_quantum_states.py
+++ b/testing/test_final_quantum_states.py
@@ -19,18 +19,17 @@ import pytest
import numpy as np
from importlib import import_module
-
+from .conftest import TESTING_BACKENDS
from .qumat_helpers import get_qumat_example_final_state_vector
class TestFinalQuantumStates:
"""Test class for final quantum state comparisons between QuMat and native
implementations."""
+ @pytest.mark.parametrize("backend_name", TESTING_BACKENDS)
@pytest.mark.parametrize("initial_ket_str", ["000", "001", "010", "011"])
- def test_qiskit_final_state_vector(self, initial_ket_str):
- """Test that QuMat produces same final state as native Qiskit
implementation."""
- backend_name = "qiskit"
-
+ def test_backend_final_state_vector(self, backend_name, initial_ket_str):
+ """Test that QuMat produces same final state as native backend
implementation."""
# Import backend-specific helpers
backend_module = import_module(f".{backend_name}_helpers",
package="testing")