Skip to content

Commit

Permalink
Provide option to assume zero initial state in ansatz generation
Browse files Browse the repository at this point in the history
The ansatz begins with ZXZ decompositions, but the first RZGate
on each qubit has no effect if acting on the initial state.  So,
this adds an option to remove that parameter on each qubit.

It's left as an option since it does not make sure to do in the case
of unitary AQC.

I'm definitely open to discussing the name of the option.

TODO

- [ ] add new argument to docstring
  • Loading branch information
garrison committed Nov 2, 2024
1 parent 1d99221 commit 2ea58f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
21 changes: 17 additions & 4 deletions qiskit_addon_aqc_tensor/ansatz_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def _nonidle_qubits(qc: QuantumCircuit, /):
}


def generate_ansatz_from_circuit(qc: QuantumCircuit, /) -> tuple[QuantumCircuit, list[float]]:
def generate_ansatz_from_circuit(
qc: QuantumCircuit, /, *, assume_initial_zero_state=False
) -> tuple[QuantumCircuit, list[float]]:
"""Generate an ansatz from the two-qubit connectivity structure of a circuit."""
# FIXME: handle classical bits, measurements, resets, and barriers. maybe
# conditions too?
Expand All @@ -138,7 +140,14 @@ def set_zxz_params_from_mat(q: int, mat) -> None:
# Following the variable convention at
# https://docs.quantum.ibm.com/api/qiskit/qiskit.synthesis.OneQubitEulerDecomposer
theta, phi, lamb = decomposer.angles(mat)
for j, r in zip(free_params[q], (lamb, theta, phi)):
fp = free_params[q]
values: tuple[float, ...] = lamb, theta, phi
if len(fp) == 2:
# Must be initial gate, where the Z rotation has been dropped.
# This makes sense if we assume the input state to this ZXZ block
# is |0>.
values = values[1:]
for j, r in zip(fp, values):
initial_params[j] = r

def perform_separation(q0: int, q1: int):
Expand All @@ -164,8 +173,12 @@ def perform_separation(q0: int, q1: int):

active_qubits = sorted([qc.find_bit(q)[0] for q in _nonidle_qubits(qc)])
for q in active_qubits:
params, free_params[q] = _allocate_parameters(param_vec, 3)
initial_params.extend([np.nan] * 3)
params, free_params[q] = _allocate_parameters(
param_vec, 2 if assume_initial_zero_state else 3
)
initial_params.extend([np.nan] * len(params))
if assume_initial_zero_state:
params.insert(0, 0.0)
ansatz.append(ZXZ(params), (q,))
singles[q] = []

Expand Down
12 changes: 10 additions & 2 deletions test/test_ansatz_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@
import pytest
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import Operator, process_fidelity
from qiskit.quantum_info import Operator, Statevector, process_fidelity, state_fidelity

from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit
from qiskit_addon_aqc_tensor.ansatz_generation import KAK


def test_ansatz_from_random_circuit():
def test_ansatz_process_fidelity_from_random_circuit():
qc = random_circuit(6, 4, max_operands=2)
ansatz, initial_params = generate_ansatz_from_circuit(qc)
ansatz.assign_parameters(initial_params, inplace=True)
fidelity = process_fidelity(Operator(ansatz), Operator(qc))
assert fidelity == pytest.approx(1)


def test_ansatz_state_fidelity_from_random_circuit():
qc = random_circuit(6, 4, max_operands=2)
ansatz, initial_params = generate_ansatz_from_circuit(qc, assume_initial_zero_state=True)
ansatz.assign_parameters(initial_params, inplace=True)
fidelity = state_fidelity(Statevector(ansatz), Statevector(qc))
assert fidelity == pytest.approx(1)


def test_ansatz_fails_given_three_qubit_gate():
qc = QuantumCircuit(3)
qc.h(0)
Expand Down

0 comments on commit 2ea58f8

Please sign in to comment.