Skip to content

Commit

Permalink
Add global phase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Sep 19, 2020
1 parent 0b71cb5 commit b618de7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 66 deletions.
27 changes: 25 additions & 2 deletions test/terra/backends/statevector_simulator/statevector_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
StatevectorSimulator Integration Tests
"""

from numpy import exp, pi

from test.terra.reference import ref_measure
from test.terra.reference import ref_reset
from test.terra.reference import ref_initialize
Expand All @@ -23,8 +25,7 @@
from test.terra.reference import ref_unitary_gate
from test.terra.reference import ref_diagonal_gate

from qiskit import execute
from qiskit.compiler import assemble
from qiskit import execute, transpile, assemble
from qiskit.providers.aer import StatevectorSimulator


Expand Down Expand Up @@ -1260,3 +1261,25 @@ def test_cu3_gate_deterministic_waltz_basis_gates(self):
result = job.result()
self.assertSuccess(result)
self.compare_statevector(result, circuits, targets)

# ---------------------------------------------------------------------
# Test global phase
# ---------------------------------------------------------------------

def test_qobj_global_phase(self):
"""Test qobj global phase."""

circuits = ref_1q_clifford.h_gate_circuits_nondeterministic(
final_measure=False)
targets = ref_1q_clifford.h_gate_statevector_nondeterministic()

qobj = assemble(transpile(circuits, self.SIMULATOR),
shots=1, backend_options=self.BACKEND_OPTS)
# Set global phases
for i, _ in enumerate(circuits):
global_phase = (-1) ** i * (pi / 4)
qobj.experiments[i].header.global_phase = global_phase
targets[i] = exp(1j * global_phase) * targets[i]
result = self.SIMULATOR.run(qobj).result()
self.assertSuccess(result)
self.compare_statevector(result, circuits, targets, global_phase=True)
89 changes: 25 additions & 64 deletions test/terra/backends/unitary_simulator/unitary_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
UnitarySimulator Integration Tests
"""

import unittest
from test.terra import common
from numpy import exp, pi

from test.terra.reference import ref_1q_clifford
from test.terra.reference import ref_2q_clifford
from test.terra.reference import ref_non_clifford
from test.terra.reference import ref_unitary_gate
from test.terra.reference import ref_diagonal_gate

from qiskit import execute
from qiskit import execute, assemble, transpile
from qiskit.providers.aer import UnitarySimulator


Expand Down Expand Up @@ -588,7 +588,6 @@ def test_cz_gate_nondeterministic_minimal_basis_gates(self):
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)


# ---------------------------------------------------------------------
# Test cu1 gate
# ---------------------------------------------------------------------
Expand Down Expand Up @@ -1064,39 +1063,6 @@ def test_diagonal_gate(self):
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

# ---------------------------------------------------------------------
# Test cswap-gate (Fredkin)
# ---------------------------------------------------------------------
def test_cswap_gate_deterministic_default_basis_gates(self):
"""Test cswap-gate circuits compiling to backend default basis_gates."""
circuits = ref_non_clifford.cswap_gate_circuits_deterministic(
final_measure=False)
targets = ref_non_clifford.cswap_gate_unitary_deterministic()
job = execute(circuits,
self.SIMULATOR,
shots=1,
backend_options=self.BACKEND_OPTS)
result = job.result()
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

# ---------------------------------------------------------------------
# Test cu1 gate
# ---------------------------------------------------------------------
def test_cu1_gate_nondeterministic_waltz_basis_gates(self):
"""Test cu1-gate gate circuits compiling to u1,u2,u3,cx"""
circuits = ref_non_clifford.cu1_gate_circuits_nondeterministic(
final_measure=False)
targets = ref_non_clifford.cu1_gate_unitary_nondeterministic()
job = execute(circuits,
self.SIMULATOR,
shots=1,
basis_gates=['u1', 'u2', 'u3', 'cx'],
backend_options=self.BACKEND_OPTS)
result = job.result()
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

def test_cswap_gate_deterministic_minimal_basis_gates(self):
"""Test cswap-gate gate circuits compiling to u3,cx"""
circuits = ref_non_clifford.cswap_gate_circuits_deterministic(
Expand All @@ -1111,20 +1077,6 @@ def test_cswap_gate_deterministic_minimal_basis_gates(self):
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

def test_cu1_gate_nondeterministic_minimal_basis_gates(self):
""""Test cu1-gate gate circuits compiling to u3,cx"""
circuits = ref_non_clifford.cu1_gate_circuits_nondeterministic(
final_measure=False)
targets = ref_non_clifford.cu1_gate_unitary_nondeterministic()
job = execute(circuits,
self.SIMULATOR,
shots=1,
basis_gates=['u3', 'cx'],
backend_options=self.BACKEND_OPTS)
result = job.result()
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

def test_cswap_gate_deterministic_waltz_basis_gates(self):
"""Test cswap-gate gate circuits compiling to u1,u2,u3,cx"""
circuits = ref_non_clifford.cswap_gate_circuits_deterministic(
Expand Down Expand Up @@ -1152,19 +1104,6 @@ def test_cswap_gate_nondeterministic_default_basis_gates(self):
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

def test_cu1_gate_nondeterministic_default_basis_gates(self):
"""Test cu1-gate gate circuits compiling to default basis"""
circuits = ref_non_clifford.cu1_gate_circuits_nondeterministic(
final_measure=False)
targets = ref_non_clifford.cu1_gate_unitary_nondeterministic()
job = execute(circuits,
self.SIMULATOR,
shots=1,
backend_options=self.BACKEND_OPTS)
result = job.result()
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

def test_cswap_gate_nondeterministic_minimal_basis_gates(self):
"""Test cswap-gate gate circuits compiling to u3,cx"""
circuits = ref_non_clifford.cswap_gate_circuits_nondeterministic(
Expand Down Expand Up @@ -1192,3 +1131,25 @@ def test_cswap_gate_nondeterministic_waltz_basis_gates(self):
result = job.result()
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets)

# ---------------------------------------------------------------------
# Test global phase
# ---------------------------------------------------------------------

def test_qobj_global_phase(self):
"""Test qobj global phase."""

circuits = ref_1q_clifford.h_gate_circuits_nondeterministic(
final_measure=False)
targets = ref_1q_clifford.h_gate_unitary_nondeterministic()

qobj = assemble(transpile(circuits, self.SIMULATOR),
shots=1, backend_options=self.BACKEND_OPTS)
# Set global phases
for i, _ in enumerate(circuits):
global_phase = (-1) ** i * (pi / 4)
qobj.experiments[i].header.global_phase = global_phase
targets[i] = exp(1j * global_phase) * targets[i]
result = self.SIMULATOR.run(qobj).result()
self.assertSuccess(result)
self.compare_unitary(result, circuits, targets, global_phase=True)

0 comments on commit b618de7

Please sign in to comment.