示例#1
0
 def test_snapshot_instruction_to_dict(self):
     """Test snapshot instruction to dict."""
     valid_qobj = QasmQobj(
         qobj_id='12345',
         header=QobjHeader(),
         config=QasmQobjConfig(shots=1024, memory_slots=2, max_credits=10),
         experiments=[
             QasmQobjExperiment(instructions=[
                 QasmQobjInstruction(name='u1', qubits=[1], params=[0.4]),
                 QasmQobjInstruction(name='u2', qubits=[1], params=[0.4, 0.2]),
                 QasmQobjInstruction(name='snapshot', qubits=[1],
                                     snapshot_type='statevector',
                                     label='my_snap')
             ])
         ]
     )
     res = valid_qobj.to_dict(validate=True)
     expected_dict = {
         'qobj_id': '12345',
         'type': 'QASM',
         'schema_version': '1.1.0',
         'header': {},
         'config': {'max_credits': 10, 'memory_slots': 2, 'shots': 1024},
         'experiments': [
             {'instructions': [
                 {'name': 'u1', 'params': [0.4], 'qubits': [1]},
                 {'name': 'u2', 'params': [0.4, 0.2], 'qubits': [1]},
                 {'name': 'snapshot', 'qubits': [1],
                  'snapshot_type': 'statevector', 'label': 'my_snap'}
             ],
              'config': {},
              'header': {}}
         ],
     }
     self.assertEqual(expected_dict, res)
示例#2
0
 def test_snapshot_instruction_to_dict(self):
     """Test snapshot instruction to dict."""
     valid_qobj = QasmQobj(
         qobj_id="12345",
         header=QobjHeader(),
         config=QasmQobjConfig(shots=1024, memory_slots=2, max_credits=10),
         experiments=[
             QasmQobjExperiment(instructions=[
                 QasmQobjInstruction(name="u1", qubits=[1], params=[0.4]),
                 QasmQobjInstruction(
                     name="u2", qubits=[1], params=[0.4, 0.2]),
                 QasmQobjInstruction(
                     name="snapshot",
                     qubits=[1],
                     snapshot_type="statevector",
                     label="my_snap",
                 ),
             ])
         ],
     )
     res = valid_qobj.to_dict()
     expected_dict = {
         "qobj_id":
         "12345",
         "type":
         "QASM",
         "schema_version":
         "1.3.0",
         "header": {},
         "config": {
             "max_credits": 10,
             "memory_slots": 2,
             "shots": 1024
         },
         "experiments": [{
             "instructions": [
                 {
                     "name": "u1",
                     "params": [0.4],
                     "qubits": [1]
                 },
                 {
                     "name": "u2",
                     "params": [0.4, 0.2],
                     "qubits": [1]
                 },
                 {
                     "name": "snapshot",
                     "qubits": [1],
                     "snapshot_type": "statevector",
                     "label": "my_snap",
                 },
             ],
             "config": {},
             "header": {},
         }],
     }
     self.assertEqual(expected_dict, res)
    def _save_job_data_s3(self, qobj: QasmQobj, s3_bucket: Optional[str] = None,
                          extra_data: Optional[dict] = None) -> AwsSession.S3DestinationFolder:
        used_s3_bucket: str = s3_bucket or self._provider.get_default_bucket()
        s3_client = self._provider.get_s3_client()
        file = f'{self._get_job_data_s3_folder(job_id=qobj.qobj_id)}/qiskit_qobj_data.json'
        if AWSBackend._exists_file(s3_client, used_s3_bucket, file):
            raise ValueError(f"An object '{file}' already exists at the bucket {used_s3_bucket}")

        body = {
            'qobj_id': qobj.qobj_id,
            'qobj': qobj.to_dict()
        }
        if extra_data:
            body['extra_data'] = extra_data

        result = s3_client.put_object(Body=json.dumps(body).encode(), Bucket=used_s3_bucket, Key=file)
        # TODO: error handling
        return used_s3_bucket, self._get_job_data_s3_folder(job_id=qobj.qobj_id)
示例#4
0
    def test_gate_calibrations_to_dict(self):
        """Test gate calibrations to dict."""

        pulse_library = [PulseLibraryItem(name="test", samples=[1j, 1j])]
        valid_qobj = QasmQobj(
            qobj_id="12345",
            header=QobjHeader(),
            config=QasmQobjConfig(shots=1024,
                                  memory_slots=2,
                                  max_credits=10,
                                  pulse_library=pulse_library),
            experiments=[
                QasmQobjExperiment(
                    instructions=[
                        QasmQobjInstruction(name="u1",
                                            qubits=[1],
                                            params=[0.4])
                    ],
                    config=QasmQobjConfig(
                        calibrations=QasmExperimentCalibrations(gates=[
                            GateCalibration(name="u1",
                                            qubits=[1],
                                            params=[0.4],
                                            instructions=[])
                        ])),
                )
            ],
        )
        res = valid_qobj.to_dict()
        expected_dict = {
            "qobj_id":
            "12345",
            "type":
            "QASM",
            "schema_version":
            "1.3.0",
            "header": {},
            "config": {
                "max_credits": 10,
                "memory_slots": 2,
                "shots": 1024,
                "pulse_library": [{
                    "name": "test",
                    "samples": [1j, 1j]
                }],
            },
            "experiments": [{
                "instructions": [{
                    "name": "u1",
                    "params": [0.4],
                    "qubits": [1]
                }],
                "config": {
                    "calibrations": {
                        "gates": [{
                            "name": "u1",
                            "qubits": [1],
                            "params": [0.4],
                            "instructions": []
                        }]
                    }
                },
                "header": {},
            }],
        }
        self.assertEqual(expected_dict, res)
示例#5
0
    def test_gate_calibrations_to_dict(self):
        """Test gate calibrations to dict."""

        pulse_library = [PulseLibraryItem(name='test', samples=[1j, 1j])]
        valid_qobj = QasmQobj(
            qobj_id='12345',
            header=QobjHeader(),
            config=QasmQobjConfig(shots=1024,
                                  memory_slots=2,
                                  max_credits=10,
                                  pulse_library=pulse_library),
            experiments=[
                QasmQobjExperiment(
                    instructions=[
                        QasmQobjInstruction(name='u1',
                                            qubits=[1],
                                            params=[0.4])
                    ],
                    config=QasmQobjConfig(
                        calibrations=QasmExperimentCalibrations(gates=[
                            GateCalibration(name='u1',
                                            qubits=[1],
                                            params=[0.4],
                                            instructions=[])
                        ])))
            ])
        res = valid_qobj.to_dict(validate=True)
        expected_dict = {
            'qobj_id':
            '12345',
            'type':
            'QASM',
            'schema_version':
            '1.3.0',
            'header': {},
            'config': {
                'max_credits': 10,
                'memory_slots': 2,
                'shots': 1024,
                'pulse_library': [{
                    'name': 'test',
                    'samples': [1j, 1j]
                }]
            },
            'experiments': [{
                'instructions': [{
                    'name': 'u1',
                    'params': [0.4],
                    'qubits': [1]
                }],
                'config': {
                    'calibrations': {
                        'gates': [{
                            'name': 'u1',
                            'qubits': [1],
                            'params': [0.4],
                            'instructions': []
                        }]
                    }
                },
                'header': {}
            }],
        }
        self.assertEqual(expected_dict, res)