Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final time calculation in _concatenate_pulses. #162

Merged
merged 4 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/qutip_qip/compiler/gatecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def _concatenate_pulses(
compiled_tlist[pulse_ind].append(execution_time)
compiled_coeffs[pulse_ind].append(coeffs)

final_time = np.max([tlist[-1] for tlist in compiled_tlist])
final_time = np.max([tlist[-1][-1] for tlist in compiled_tlist])
for pulse_ind in range(num_controls):
if not compiled_tlist[pulse_ind]:
continue
Expand Down
52 changes: 49 additions & 3 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,57 @@ def test_compiling_with_scheduler():
assert(abs(time_scheduled2 * 2 - time_not_scheduled) < 1.0e-10)


def gauss_dist(t, sigma, amplitude, duration):
return amplitude/np.sqrt(2*np.pi) /sigma*np.exp(-0.5*((t-duration/2)/sigma)**2)
def test_compiling_gates_different_sampling_number():
"""
Define compiler without a physical model.
Test compiling gates to pulses with different sampling number.
"""

class MockCompiler(GateCompiler):
def __init__(self, num_qubits, params=None):
super().__init__(num_qubits, params=params)
self.gate_compiler["U1"] = self.single_qubit_gate_compiler
self.gate_compiler["U2"] = self.two_qubit_gate_compiler
self.args.update({"params": params})

def single_qubit_gate_compiler(self, gate, args):
pulse_info = [("x", np.array([1.0] * 3))]
return [
Instruction(
gate, tlist=np.linspace(0, 2, 3), pulse_info=pulse_info
)
]

def two_qubit_gate_compiler(self, gate, args):
pulse_info = [("xx", np.array([2.0] * 5))]
return [
Instruction(
gate, tlist=np.linspace(0, 4, 5), pulse_info=pulse_info
)
]

num_qubits = 2
circuit = QubitCircuit(num_qubits)
circuit.add_gate("U1", targets=0, arg_value=1.0)
circuit.add_gate("U2", targets=[0, 1], arg_value=1.0)
circuit.add_gate("U1", targets=0, arg_value=1.0)

compiler = MockCompiler(num_qubits=2)
compiled_tlists, compiled_coeffs = compiler.compile(circuit)

# Filter out the nonzero part of the pulse
# and check if they are correct.
np.testing.assert_array_equal(
compiled_tlists["x"][np.nonzero(compiled_coeffs["x"])[0]],
np.array([1, 2, 7, 8]),
)
np.testing.assert_array_equal(
compiled_tlists["xx"][np.nonzero(compiled_coeffs["xx"])[0]],
np.array([3, 4, 5, 6]),
)


from qutip_qip.compiler import GateCompiler
# Test the compiler with a physical model.
class MyCompiler(GateCompiler): # compiler class
def __init__(self, num_qubits, params):
super(MyCompiler, self).__init__(num_qubits, params=params)
Expand Down