Skip to content

Commit

Permalink
Merge branch 'main' into PR-MKL
Browse files Browse the repository at this point in the history
  • Loading branch information
doichanj authored Jan 30, 2024
2 parents dbd99b7 + 6f375e3 commit 07794b4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 14 deletions.
70 changes: 69 additions & 1 deletion test/terra/backends/aer_simulator/test_conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"""
AerSimulator Integration Tests
"""

from ddt import ddt
from test.terra.reference import ref_conditionals
from test.terra.backends.simulator_test_case import SimulatorTestCase, supported_methods

from qiskit import QuantumCircuit
from qiskit.circuit.library import DiagonalGate

@ddt
class TestConditionalGates(SimulatorTestCase):
Expand Down Expand Up @@ -310,3 +311,70 @@ def test_conditional_superop_132bit(self, method, device):
result = backend.run(circuits, shots=shots).result()
self.assertSuccess(result)
self.compare_counts(result, circuits, targets, hex_counts=False, delta=0)

@ddt
class TestConditionalReset(SimulatorTestCase):
"""AerSimulator conditional reset tests."""

SUPPORTED_METHODS = [
"automatic",
"statevector",
"density_matrix",
"matrix_product_state",
"tensor_network",
]

# ---------------------------------------------------------------------
# Test conditional
# ---------------------------------------------------------------------
@supported_methods(SUPPORTED_METHODS)
def test_conditional_reset_1bit(self, method, device):
"""Test conditional reset on 1-bit conditional register."""
shots = 100
backend = self.backend(method=method, device=device)
backend.set_options(max_parallel_experiments=0)

circuits = ref_conditionals.conditional_circuits_1bit(
final_measure=True, conditional_type="reset"
)
targets = ref_conditionals.conditional_counts_1bit_with_reset(shots)
result = backend.run(circuits, shots=shots).result()
self.assertSuccess(result)
self.compare_counts(result, circuits, targets, delta=0)


@ddt
class TestConditionalDiagonal(SimulatorTestCase):
"""AerSimulator conditional diagonal tests."""

# ---------------------------------------------------------------------
# Test conditional
# ---------------------------------------------------------------------
def test_conditional_diagonal(self):
"""Test conditional diagonal with statevector."""
shots = 100
backend = self.backend(method="statevector", device="CPU")
backend.set_options(max_parallel_experiments=0)

circuit = QuantumCircuit(4, 4)
for i in range(1, 4):
circuit.h(i)
circuit.save_statevector(label="base")

circuit0 = QuantumCircuit(4, 4)
for i in range(1, 4):
circuit0.h(i)
circuit0.append(DiagonalGate([-1, -1]), [1]).c_if(circuit0.clbits[0], 0)
circuit0.save_statevector(label="diff")

circuit1 = QuantumCircuit(4, 4)
for i in range(1, 4):
circuit1.h(i)
circuit1.append(DiagonalGate([-1, -1]), [1]).c_if(circuit1.clbits[0], 1)
circuit1.save_statevector(label="equal")

result = backend.run([circuit, circuit0, circuit1], shots=1).result()
self.assertSuccess(result)

self.assertNotEqual(result.data(circuit)["base"], result.data(circuit0)["diff"])
self.assertEqual(result.data(circuit)["base"], result.data(circuit1)["equal"])
11 changes: 4 additions & 7 deletions test/terra/reference/ref_diagonal_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

# Backwards compatibility for Terra <= 0.13
if not hasattr(QuantumCircuit, "diagonal"):
QuantumCircuit.diagonal = QuantumCircuit.diag_gate
from qiskit.circuit.library import DiagonalGate


def diagonal_gate_circuits_deterministic(final_measure=True):
Expand All @@ -41,7 +38,7 @@ def diagonal_gate_circuits_deterministic(final_measure=True):
for diag in [arg, np.array(arg), np.array(arg, dtype=float), np.array(arg, dtype=complex)]:
circuit = QuantumCircuit(*regs)
circuit.h(qubit)
circuit.diagonal(list(diag), [qubit])
circuit.append(DiagonalGate(diag), [qubit])
circuit.h(qubit)
if final_measure:
circuit.barrier(qr)
Expand All @@ -53,7 +50,7 @@ def diagonal_gate_circuits_deterministic(final_measure=True):
for diag in [arg, np.array(arg), np.array(arg, dtype=float), np.array(arg, dtype=complex)]:
circuit = QuantumCircuit(*regs)
circuit.h(qr)
circuit.diagonal(list(diag), qr)
circuit.append(DiagonalGate(diag), qr)
circuit.h(qr)
if final_measure:
circuit.barrier(qr)
Expand All @@ -64,7 +61,7 @@ def diagonal_gate_circuits_deterministic(final_measure=True):
for diag in [np.array([1, 1, 1, np.exp(-1j * np.pi / k)]) for k in [10, 100, 1000, 10000]]:
circuit = QuantumCircuit(*regs)
circuit.x(qr)
circuit.diagonal(list(diag), qr)
circuit.append(DiagonalGate(diag), qr)
if final_measure:
circuit.barrier(qr)
circuit.measure(qr, cr)
Expand Down
5 changes: 3 additions & 2 deletions test/terra/reference/ref_multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ccx_gate_counts_deterministic,
)
from qiskit.quantum_info.states import Statevector
from qiskit.circuit.library import Isometry, UCGate


def multiplexer_cx_gate_circuits_deterministic(final_measure=True):
Expand Down Expand Up @@ -289,14 +290,14 @@ def multiplexer_no_control_qubits(final_measure=True):
qc = QuantumCircuit(1, 1)
vector = [0.2, 0.1]
vector_circuit = QuantumCircuit(1)
vector_circuit.isometry(vector / np.linalg.norm(vector), [0], None)
vector_circuit.append(Isometry(vector / np.linalg.norm(vector), 0, 0), [0])
vector_circuit = vector_circuit.inverse()
qc.append(vector_circuit, [0])

sv = Statevector(qc)
gate_list = [np.array([[sv[0], -sv[1]], [sv[1], sv[0]]])]
qc = QuantumCircuit(1, 1)
qc.uc(gate_list, [], [0])
qc.append(UCGate(gate_list), [0])

if final_measure:
qc.measure(0, 0)
Expand Down
8 changes: 5 additions & 3 deletions test/terra/states/test_aer_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from qiskit.circuit import QuantumCircuit, Gate
from qiskit.quantum_info.random import random_unitary
from qiskit.quantum_info import random_statevector, random_density_matrix
from qiskit.circuit.library import DiagonalGate

from qiskit_aer import AerSimulator

from test.terra import common
Expand Down Expand Up @@ -375,9 +377,9 @@ def test_appply_diagonal(self):

circuit = QuantumCircuit(5)
circuit.initialize(init_state, [0, 1, 2, 3, 4])
circuit.diagonal(diag_1, [0])
circuit.diagonal(diag_2, [1, 2])
circuit.diagonal(diag_3, [3, 4, 0])
circuit.append(DiagonalGate(diag_1), [0])
circuit.append(DiagonalGate(diag_2), [1, 2])
circuit.append(DiagonalGate(diag_3), [3, 4, 0])
circuit.save_statevector()

aer_simulator = AerSimulator(method="statevector")
Expand Down
3 changes: 2 additions & 1 deletion test/terra/states/test_aer_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from qiskit.quantum_info.operators.predicates import matrix_equal
from qiskit.visualization.state_visualization import numbers_to_latex_terms, state_to_latex
from qiskit.circuit.library import QFT, HGate
from qiskit.circuit.library import DiagonalGate

from test.terra import common
from qiskit_aer import AerSimulator
Expand Down Expand Up @@ -289,7 +290,7 @@ def test_diagonal(self):
circuit = QuantumCircuit(3)
circuit.h(range(3))
diagonal = [1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, -1.0]
circuit.diagonal(diagonal, list(range(3)))
circuit.append(DiagonalGate(diagonal), list(range(3)))
target = AerStatevector.from_label("000").evolve(Operator(circuit))
psi = AerStatevector.from_instruction(circuit)
self.assertEqual(psi, target)
Expand Down

0 comments on commit 07794b4

Please sign in to comment.